0

im trying to get a nice output of direct reports of a user with a table.

The code below gets me the output but it shows me the distinguishedName and not the displayname.

Get-ADUser $user -Properties * | Select-Object -ExpandProperty DirectReports

I would like to have it to be something like this but with Direct reports instead of Manager

Get-ADUser -Identity $user -Properties * | select-object @{N='Manager';E={(Get-ADUser ($_.Manager)).name}} | Format-List

I got it to the point where i can list the direct reports but they are not row by row and i want to have only the display name

get-aduser -Identity $user -Properties DirectReports | Select-Object name, @{N='Direct reports';E="DirectReports"} | format-list
PixPaw
  • 23
  • 5

1 Answers1

0

Because this:

get-aduser -Identity $user -Properties DirectReports | Select-Object name, @{N='Direct reports';E="DirectReports"} | format-list

... does not do that. You asked for an alist, not a columnar report, i.e., Csv/txt. That is what Export-Csv is for or hash-table, PSCustomObject.

What you are after is not new, as it is a common thing, with many examples all over the web, and via the SO searchbox above.

PowerShell - Who Reports to Whom? (Active Directory recursive DirectReports)

# Find all direct user reporting to Test_director
Get-ADDirectReports -Identity Test_director

# Find all Indirect user reporting to Test_director
Get-ADDirectReports -Identity Test_director -Recurse

Get-ADUser -Identity test_director -Properties directreports |
    Select-Object -ExpandProperty directreports |
    Get-ADUser -Properties mail |
    Select-Object SamAccountName, mail

Get Direct Reports in Active Directory Using Powershell (Recursive)

Function Get-DirectReport { #requires -Module ActiveDirectory

<#
.SYNOPSIS
    This script will get a user's direct reports recursively from ActiveDirectory unless specified with the NoRecurse parameter.
    It also uses the user's EmployeeID attribute as a way to exclude service accounts and/or non standard accounts that are in the reporting structure.
  
.NOTES
    Name: Get-DirectReport
    Author: theSysadminChannel
    Version: 1.0
    DateCreated: 2020-Jan-28
  
.LINK
    https://thesysadminchannel.com/get-direct-reports-in-active-directory-using-powershell-recursive -   
  
.PARAMETER SamAccountName
    Specify the samaccountname (username) to see their direct reports.
  
.PARAMETER NoRecurse
    Using this option will not drill down further than one level.
  
.EXAMPLE
    Get-DirectReport username
  
.EXAMPLE
    Get-DirectReport -SamAccountName username -NoRecurse
  
.EXAMPLE
    "username" | Get-DirectReport
#>
 
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
 
        [string]  $SamAccountName,
 
        [switch]  $NoRecurse
    )
 
    BEGIN {}
 
    PROCESS {
        $UserAccount = Get-ADUser $SamAccountName -Properties DirectReports, DisplayName
        $UserAccount | select -ExpandProperty DirectReports | ForEach-Object {
            $User = Get-ADUser $_ -Properties DirectReports, DisplayName, Title, EmployeeID
            if ($null -ne $User.EmployeeID) {
                if (-not $NoRecurse) {
                    Get-DirectReport $User.SamAccountName
                }
                [PSCustomObject]@{
                    SamAccountName     = $User.SamAccountName
                    UserPrincipalName  = $User.UserPrincipalName
                    DisplayName        = $User.DisplayName
                    Manager            = $UserAccount.DisplayName
                }
            }
        }
    }
 
    END {}
 
}

How to get the number of direct and indirect reports of a manager from AD in Powershell

postanote
  • 15,138
  • 2
  • 14
  • 25