Solved

Powershell Get-ADuser export OU name

Posted on 2013-01-22
4
9,120 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
Comment Utility
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

0
 

Author Comment

by:pacch
Comment Utility
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
Comment Utility
Replace @{n='OU';e={$_.canonicalname -replace "/$($_.cn)",""}} with
@{n='OU';e={($_.canonicalname -Split "/")[-2]}}
0
 

Author Closing Comment

by:pacch
Comment Utility
Thanks !!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now