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
Solved

powershell script to get OU of computer from txt file

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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