Thursday, 11 August 2016

Extract OU Information of Users - Computers - Groups

Generating AD report with OU information not the Distinguished name of the object: 

Request:

I have been requested to provide the list of users in domain with their current OU location information. It's just simple for every admin to generate the complete list of users using native tools like dsquery or using easy to use Powershell cmdlets like ( Get-aduser )

The tricky part is that we are expecting the OU information but you know we generally tend to select the distinguished name but you know it's like tricky to extract to remove the CN information from the report generated and have to work in Excel sheet to get the desired output. 

Few days ago, I got similar request and extract the objects from AD and did played with excel to extract only the OU information and samaccount name.

We are in Powershell world where these type of requests can be easily handled without additional work in excel workbook.

Solution:

Use Powershell.....

get-content ".\users.txt" |get-aduser -pr distinguishedname,samaccountname -errorAction silentlycontinue |select samaccountname, @{name="OU";expression={($_.DistinguishedName -split ",",2)[1]}}| sort samaccountname |Export-Csv Users_OU_Information.csv -notypeinformation


The main command which we need focus is about : @{name="OU";expression={($_.DistinguishedName -split ",",2)[1]}} 

This command actually gets the distinguished name attribute and split the CN information which is separated by comma ","  

Similarly we can  use this similar command in groups OU information or Computers OU information. 

May be this will be handy when you need it... 

Have a Great Day !!!

1 comment: