Hello
Thank you for your question and reaching out. I can understand you are having query related to Get-ADUser List All Direct Report Recursively
You can try below PowerShell Function to get Direct report
Function GetManager($Manager, $Report)
{
# Output this manager and direct report.
"""$Manager"",""$Report""" | Out-File -FilePath $File -Append
# Find the manager of this manager.
$User = [ADSI]"LDAP://$Manager"
$NextManager = $User.manager
If ($NextManager -ne $Null)
{
# Check for circular hierarchy.
If ($NextManager -eq $Report) {"Circular hierarchy found with $Report"}
Else
{
GetManager $NextManager $Report
}
}
}
$D = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Domain = [ADSI]"LDAP://$D"
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.PageSize = 200
$Searcher.SearchScope = "subtree"
$Searcher.PropertiesToLoad.Add("distinguishedName") > $Null
$Searcher.PropertiesToLoad.Add("manager") > $Null
$Searcher.SearchRoot = "LDAP://" + $Domain.distinguishedName
$File = ".\ADOrganization.csv"
"Organization: $D" | Out-File -FilePath $File
"Manager,Direct Report" | Out-File -FilePath $File -Append
Find all direct reports, objects with a manager.
$Filter = "(manager=*)"
Run the query.
$Searcher.Filter = $Filter
$Results = $Searcher.FindAll()
ForEach ($Result In $Results)
{
$ReportDN = $Result.Properties.Item("distinguishedName")
$ManagerDN = $Result.Properties.Item("manager")
GetManager $ManagerDN $ReportDN
}
--------------------------------------------------------------------------------------------------------------------------------------------------
--If the reply is helpful, please Upvote and Accept as answer--