Solved

Powershell Get-ADuser export OU name

Posted on 2013-01-22
4
12,342 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import CSV with All modify groups 17 89
ADFS: AADSTS50107: Requested federation realm object 8 109
Powershell - Setting GPO 8 50
WinRM from Domain PC to workgroup PC 4 57
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
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…

739 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