Powershell query for OU membership

leadtheway
leadtheway used Ask the Experts™
on
I have a list of computers in a text file and I want to query AD to pull the OU they belong to.  Is this possible, i know to query ad to pull the computer from the OU, but i want to take a preexisting list and query to get OU membership
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Emmanuel AdebayoGlobal Windows Infrastructure Engineer - Consultant
Top Expert 2013

Commented:
Dsquery command will do this for you perfectly

Author

Commented:
but I'd have to do one at a time right?
Emmanuel AdebayoGlobal Windows Infrastructure Engineer - Consultant
Top Expert 2013
Commented:
Put all the server in a text file and parse this to dsquery command.

Author

Commented:
Can you help with the syntax, I'm not familiar with dsquery, mostly use PS
Top Expert 2014
Commented:
I'd just look at the distinguishedName, as that shows the OU.  If you absolutely need just the OU then you'd want to parse the DN to strip off the computer name.
Get-Content computers.txt | Get-ADComputer | Select distinguishedName

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial