Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Get-ADuser export OU name

Posted on 2013-01-22
4
Medium Priority
?
14,907 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

730 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