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