Reputation: 91
I have a list of servers and need to find out what OU each one is in. I have searched and found something close but cannot get it to work properly. I am thinking there is a relatively easy way to do this but it eludes me. I have been trying to use the script found at the following link: https://gallery.technet.microsoft.com/scriptcenter/Retrieve-OU-for-Computer-7a062402 Does anyone know of an easy way to do this having it pull from a txt file using a foreach and querying against AD successfully
I tried a simple get-adcomputer servername which gave me a bunch of info including the OU, I want to build on this so it gives me just the OU for a list of servers
Thank you in advance for your time.
Upvotes: 1
Views: 10934
Reputation: 1660
You said you got a bunch of info including the OU. I presume you mean the DistinguishedName
, which is one property selected as default by Get-ADComputer
. If you have the names of the servers in a text file computers.txt
, you could do this:
Get-Content .\computers.txt | ForEach-Object {Get-ADComputer $_} | select DistinguishedName
Upvotes: 1