Solved

Powershell Get-ADuser export OU name

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

1
 

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]}}
0
 

Author Closing Comment

by:pacch
ID: 38805272
Thanks !!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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