Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

powershell script to get OU of computer from txt file

Posted on 2011-03-18
5
Medium Priority
?
2,699 Views
Last Modified: 2012-06-27
Hello,

I need a powershell script to get from a txt file with a list of computer the OU for every computer and export the result to a csv file.

This is the format of my txt file:

server1
server2
.....

thanks
0
Comment
Question by:cawasaki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 total points
ID: 35164423
Active Directory module version (Windows Server 2008 R2):

Import-Module activedirectory
get-content servers.txt | Get-ADComputer | Select-Object name, @{n="OU"; e={$_.distinguishedname -replace "CN=[^,]+,",""}} | Export-Csv c:\someplace\computersandOUs.txt -NoTypeInformation

Quest's ActiveRoles AD snapin version:

Add-PSSnapin Quest.ActiveRoles.ADManagement
get-content servers.txt | Get-QADComputer | Select-Object name, @{n="OU"; e={$_.dn -replace "CN=[^,]+,",""}} | Export-Csv c:\someplace\computersandOUs.txt -NoTypeInformation
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35164462
A bit more improved ones:

Import-Module activedirectory
get-content servers.txt | Get-ADComputer | Select-Object name, @{n="OU"; e={$_.distinguishedname -replace "^CN=[^,]+,",""}} | Export-Csv c:\someplace\computersandOUs.txt -NoTypeInformation

Quest's ActiveRoles AD snapin version:

Add-PSSnapin Quest.ActiveRoles.ADManagement
get-content servers.txt | Get-QADComputer | Select-Object name, @{n="OU"; e={$_.dn -replace "^CN=[^,]+,",""}} | Export-Csv c:\someplace\computersandOUs.txt -NoTypeInformation
0
 

Author Comment

by:cawasaki
ID: 35164620
Hi soostibi,

its the same script!
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35165337
No, there is an extra ^ sign after the quatation mark.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 35165405
another option with the quest cmdlets you can get the parent container instead of doing the replace

Get-QADComputer | Select-Object name, ParentContainerDN
Get-QADComputer | Select-Object name, ParentContainer
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question