powershellactive-directory

Attempting to sort out Manager Name from Get-ADUser


Hello I'm currently trying to have my Get-ADUser expression to sort the Manager to just display the managers name. I have search for multiple solutions however nothing that matches what I've been trying to do with have all my properties listed as shown.

Get-ADUser $username  -Properties Enabled,employeeNumber,DisplayName,Mail,Description,office,extensionAttribute1,manager,AccountExpirationDate | 
    Select-Object Enabled, 
    @{Expression={$_.DisplayName};Label='Display Name';},
    @{Expression={$_.Mail};Label='Email';}, 
    @{Expression={$_.employeeNumber};Label='Employee Number'},
    @{Expression={$_.AccountExpirationDate};Label='Account Expiration Date'},
    @{Expression={$_.Description};Label='Account Description'}, 
    Office,
    @{Expression={$_.extensionAttribute1};Label='Cost Center';},
    Manager
    Format-list

Currently the output is:

Manager: CN=Billy Bob Manager,OU=Users,OU=MAHRS,OU=Locations,OU=Test,DC=corp,DC=TestDC,DC=com

I would like to know how I could keep everything under the original Get-ADUser.

Thanks!


Solution

  • You have 2 simple options, either query AD for the Manager's .Name attribute:

    @{ Expression = { (Get-ADUser $_.Manager).Name }; Label = 'Manager' }
    

    Or extract the cn from the DistinguishedName with regex:

    @{ Expression = { $_.Manager -replace '^CN=|(?<!\\),.+' }; Label = 'Manager' }
    

    Relevant documentations: