Author Posts

February 4, 2016 at 4:12 pm

I was able to piece together a script. This script will query the AD and return a list which is needed. The problem we had was when we used manager it returned this very long list and (the entire OU path), and what we really wanted was the managers email address.

I searched and found some code that would give the samaccountname. I figured easy enough to change that to the attribute "mail" but when I do modify it nothing is returned for manager.

we was able to use the script below and then HR concatenated the end of the email onto the samaccountname in excel so we found a work around. However, as a young powershell user learning the basics its driving me crazy that I cannot get this to just give the email =(

any ideas?

Below is the code.

———————–
$props = @('employeeNumber', 'Name', 'mail', 'manager')                                  
 

$ous=@("ou=testfolder,dc=test,dc=net,dc=int")
 
$ous | ForEach {
  Get-ADUser -Properties $props -Filter 'Enabled -eq $true' -SearchBase $_ | Select employeeNumber, name, mail, @{Name='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}}
} |export-csv -NoTypeInformation c:\scripts\HRdata.csv

———————-

The portion that was new to this script was the following @{Name='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}}

I tried as stated earlier to change it to @{Name='Manager';Expression={(Get-ADUser $_.Manager).mail}}

However that would not work for some reason.

Everything else I have been using for awhile now to pull up information on attributes.

February 4, 2016 at 4:30 pm

Try this. You have to use -property mail in your expression otherwise by default it won't show this

$props = @('Name', 'mail', 'manager')
$ous=@("ou=test,dc=abc,dc=test,dc=com")

$ous | ForEach {
Get-ADUser -Properties $props -Filter 'Enabled -eq $true' -SearchBase $_ | Select employeeNumber, name, mail, @{Name='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}},@{Name='Manager email';Expression={(Get-ADUser $_.Manager -Properties mail).mail}} | ft
}

February 4, 2016 at 4:34 pm

Also, you can just use get-aduser | gm to list all the properties

February 4, 2016 at 4:59 pm

Thank you . That worked. I just didnt fully understand some of the script I was using. that cleared it up for me!

Back to studying my don Jones powershell books! the script I am working with is way ahead of where my studies are.

Thanks again Yuan!

February 4, 2016 at 5:24 pm

edited this since your above answer actually answers all my questions in full once I thought about it.