?
Solved

powershell script to get OU of computer from txt file

Posted on 2011-03-18
5
Medium Priority
?
2,597 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

762 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