1

I extract with my script the users of my active directory

Import-Module ActiveDirectory
Get-ADUser -Filter * -properties * | Select SAMaccountname, givenname, surname, DistinguishedName | Export-Csv c: \ users \ administrator \ desktop \ users.csv -notypeinformation -Encoding UTF8

In this script I use the DistinguishedName command which returns the CN, the OU and the DC. My goal is to keep only the first OU and delete the rest when creating the .csv.

Example of what the script returns:

"SAMaccountname","givenname","surname","DistinguishedName"
"Jean-Yves.R","Jean-Yves","Raymond","CN=Jean-Yves Raymond,OU=Communication,OU=Direction Générale,OU=Elan & Co,OU=Domain Controllers,DC=ELAN-G1,DC=local"

Example of what I want:

"SAMaccountname","givenname","surname","DistinguishedName"
"Jean-Yves.R","Jean-Yves","Raymond","Communication"

TY :)

4
  • Use the filter parameter, and specify the OU you wish to query for (Communication). Commented Apr 19, 2017 at 11:39
  • ...Or just split the DN property? Commented Apr 19, 2017 at 11:41
  • Do you have an example to give me with the filter please? ty ^^ Commented Apr 19, 2017 at 11:45
  • stop using -properties *. It is horribly slow since it returns everything, even the non indexed properties, and you are only using the properties returned by default anyways. It is not needed. If you do need more properties just ask for the ones you want. -Properties employeeid Commented Apr 19, 2017 at 12:14

2 Answers 2

1

This will give you the OU property you want.

Get-ADUser -Filter * -properties * | Select SAMaccountname, givenname, surname,@{Name='OU';Expression={$($_.DistinguishedName).Split(",")[1].Replace("OU=","")}}

EDIT:

To deal with the Users OU:

Get-ADUser -Filter * -properties * | Select SAMaccountname, givenname, surname,@{Name='OU';Expression={$($_.DistinguishedName).Split(",")[1].Replace("OU=","").Replace("CN=","")}}
Sign up to request clarification or add additional context in comments.

3 Comments

Great ! It works perfectly if the user is in an OU create but if the user is just in "user" it shows me this: "Juny.R","Juny","Rab","CN=Users" It should be deleted "CN="
@Chrys Just add another replace in that case. You can chain them together.
OK :) Your the best ^^ TY very much
0
Get-ADUser -Filter * -properties * | Select SAMaccountname, givenname, surname,@{l='DistinguishedName';e={([adsi]"LDAP://$($_.DistinguishedName)").Parent}}

This uses a calculated property within the Select statement to get the Parent property of the DistinguishedName via LDAP.

2 Comments

Hello :) It returns me this : "Jean-Yves.R","Jean-Yves","Raymond","LDAP://OU=Communication,OU=Direction Générale,OU=Elan & Co,OU=Domain Controllers,DC=ELAN-G1,DC=local"
Are you sure you copied it exactly as above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.