Get-ADUser List All Direct Report Recursively

Shawn 6 Reputation points
2022-06-17T09:35:34.11+00:00

We have a request to get all direct reports recurrsivey of a manager. Let's say manager A has 20 directs reports who may also have several direct reports....The query ends with someone who without any subordinates.

I can't figure out how to achieve that deep through PowerShell. Any help would be appreciated. Thanks!

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Anonymous
    2022-06-20T04:00:58.027+00:00

    Hi @Shawn ,

    You may have to write a recursive function of your own. It could be something like this

    function Get-AllReports {  
        param(  
            [string]$Manager  
        )   
        $DirectReports = Get-ADUser -Filter {manager -eq $Manager}  
        if($DirectReports){  
            Write-Output $DirectReports  
            $DirectReports | ForEach-Object {  
                Get-AllReports -Manager $_.DistinguishedName  
            }      
        }  
    }  
      
    $ManagerName = "manager1"  
    Get-AllReports -Manager $ManagerName  
    

    Best Regards,
    Ian Xue

    -----------------------------

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Shawn 6 Reputation points
    2022-07-02T10:49:32.123+00:00

    I wrote the following PS srcipt to get my issue resolved finally.

    ----------

    #Define A New Function "Get-DirectReports"#  
    function Get-DirectReports ($manager)   
    {  
        $reports = Get-ADUser -Identity $manager -Properties directreports | select-object -ExpandProperty DirectReports  
        foreach ($user in $reports) {  
            Get-DirectReports $user  
        }  
        if ($reports) {  
            return $reports  
        }  
    }  
      
    #Start Quering#  
    $manager = "samaccountname"  
    $DirectReports = Get-DirectReports $manager | ForEach-Object   
    {  
        Get-ADUser -identity $_ | Select-Object samAccountName,userprincipalname  
      
    }  
      
    #Output#  
    $DirectReports | Export-csv $home\desktop\DirectReports_manager_date.csv -NoTypeInformation -Encoding UTF8  
    

    ----------

    1 person found this answer helpful.
    0 comments No comments

  3. rr-4098 2,051 Reputation points
    2022-06-18T10:13:34.673+00:00

    Please see if the following article points you in the right direction:
    https://office365itpros.com/2021/03/30/report-manager-direct-reports/


  4. Limitless Technology 39,926 Reputation points
    2022-06-20T08:40:24.163+00:00

    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--

    0 comments No comments

  5. Rich Matheisen 47,901 Reputation points
    2022-06-20T14:42:28.913+00:00

    This may be what you're looking for. If it's not it should at least give you an example of how to do what you ask.

    If you want a graphic representation of this (i.e., an Org Chart), then Microsoft's Visio is probably the easiest way: 818410

    30104.active-directory-documenting-your-ad-organization-with-powershell.aspx

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.