Solved

powershell script to get OU of computer from txt file

Posted on 2011-03-18
5
2,386 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
  • 3
5 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 500 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

823 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