Solved

Powershell Get-ADuser export OU name

Posted on 2013-01-22
4
11,232 Views
Last Modified: 2013-01-22
Hi,
Is it possible to get the name of the child OU where the user is located written to the file.csv with GetADuser

I tried with ",OU" as shown below but that returned this "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection"

Get-ADUser -Filter {mail -like '*@*'} -SearchBase "OU=myOU",DC=myDC,DC=com" -Properties * | Select-Object sAMAccountName,givenName,sn,mail,employeeType,company,Department,OU | Export-Csv "C:\file.csv" -Encoding "Unicode"

regards
0
Comment
Question by:pacch
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 38804937
Try..
Get-ADUser -Filter {mail -like '*@*'} -SearchBase "OU=myOU,DC=myDC,DC=com" -Properties * | Select-Object sAMAccountName,givenName,sn,mail,employeeType,company,Department,@{n='OU';e={$_.canonicalname -replace "/$($_.cn)",""}} | Export-Csv "C:\file.csv" -Encoding "Unicode"

or

Get-ADUser -Filter {mail -like '*@*'} -SearchBase "OU=myOU,DC=myDC,DC=com" -Properties * | Select-Object sAMAccountName,givenName,sn,mail,employeeType,company,Department,@{n='OU';e={$_.distinguishedname -replace "CN=$($_.cn),",""}} | Export-Csv "C:\file.csv" -Encoding "Unicode"

Open in new window

2
 

Author Comment

by:pacch
ID: 38805052
Thanks for the reply!

It works thanks - it is writing the entire OU path like this "myOU/child1/child2" is it possible just to get "child2"

get-ADUser -Filter {mail -like '*@*'} -SearchBase "OU=myOU,DC=myDC,DC=com" -Properties * | Select-Object sAMAccountName,givenName,sn,mail,employeeType,company,Department,@{n='OU';e={$_.canonicalname -replace "/$($_.cn)",""}} | Export-Csv "C:\file.csv" -Encoding "Unicode"

Regards
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 38805192
Replace @{n='OU';e={$_.canonicalname -replace "/$($_.cn)",""}} with
@{n='OU';e={($_.canonicalname -Split "/")[-2]}}
1
 

Author Closing Comment

by:pacch
ID: 38805272
Thanks !!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Synchronize a new Active Directory domain with an existing Office 365 tenant
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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