Arbelac
Arbelac

Reputation: 1904

Getting ad-users Manager DisplayName output

I want to fetch the manager's user object and grab the DisplayName instead of the DN.

$expiredaccounts = Search-ADAccount -AccountExpiring -TimeSpan 70.00:00:00 | Where-Object { ($_.Enabled -eq $true) }


$expiredaccounts | Select-Object name, SamAccountName, @{Name='EmployeeID';Expression={($_ |Get-ADUser -Properties employeeID).employeeID}} , @{Name='Manager';Expression={($_ |Get-ADUser -Properties manager).manager}}

Output :

name          SamAccountName    EmployeeID Manager                                                            
----          --------------    ---------- -------                                                            
User user01                                CN=User,OU=IT,DC=contoso,DC=com

Upvotes: 0

Views: 3388

Answers (1)

Robert Dyjas
Robert Dyjas

Reputation: 5227

Pipe it to Get-ADUser once again:

$expiredaccounts | Select-Object name, SamAccountName, @{Name='EmployeeID';Expression={($_ |Get-ADUser -Properties employeeID).employeeID}} , @{Name='Manager';Expression={ (($_ |Get-ADUser -Properties manager).manager | Get-ADUser).Name}}

Explanation:

You take the Manager property and run Get-ADUser against it. It returns user object of user's manager. Using .Name you extract the only required property, which is display name.

Upvotes: 1

Related Questions