Return manager's samacountname for users
active-directory, powershell
Solution
Here ya go $user being the user you are querying for manager information
(get-aduser (get-aduser $user -Properties manager).manager).samaccountName
Problem
I've got a requirement to create a CSV file of all active users from AD including the line manager attribute, however I need the line managers `sAMAccountName`, not the `cn`. Here is what I have so far: ``` Get-ADUser -server server_ip -Filter { mail -like "*" -and ObjectClass -eq "user" } ` -SearchBase "OU=Active Users,DC=eu,DC=ad,DC=some_company,DC=com" ` -Properties objectGUID,displayName,office,division,department,employeeNumber, employeeID,mobilePhone,officePhone,ipphone,title,givenName,surname,mail, manager,sAMAccountName | Export-CSV "EU_AD_Properties.csv" ``` This returns all the data I want, but gives me the line manager's `cn`, not the `samacountname`. Any ideas? I've tried this: ``` Get-ADUser -server server_ip -Filter { mail -like "*" -and ObjectClass -eq "user" } ` -SearchBase "OU=Active Users,DC=eu,DC=ad,DC=some_company,DC=com" ` -Properties objectGUID,displayName,office,division,department,employeeNumber, employeeID,mobilePhone,officePhone,ipphone,title,givenName,surname,mail, @{Label="Manager";Expression={(Get-aduser -filter {sAMAccountName -eq $_.Manager}.sAMAaccountName)}}, sAMAccountName | Export-CSV "EU_AD_Properties.csv" ``` However this errors out.