Solved

powershell script to get OU of computer from txt file

Posted on 2011-03-18
5
2,307 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A procedure for exporting installed hotfix details of remote computers using powershell
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now