powershell to get all users and their managers recursively

Michael-5966 20 Reputation points
2024-04-30T13:24:36.5566667+00:00

I am looking to pull all Active Directory users and their managers all the way up to the chain.

I get how to do get-aduser -filter * -properties * | select name, manager export-csv -path "csv path"

I also get:

$Users = Get-ADUser -filter * -Properties Manager

foreach($User in $Users){

$Manager = Get-ADUser $User.Manager -Properties DisplayName 

$ManagerName = $Manager.DisplaýName

"$($User.Name) -> $ManagerName"

}

But I am looking to get a recursive list of managers. So if I have a manager i also want my managers manager and so forth till i get all the way to the top.

manager list would look something like this:

Manager: steve | paul | mike | matt

it doesn't have to have the | to separate the manager but something like that where steve would be my direct manager and matt is the CEO.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,958 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,396 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,128 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,106 Reputation points
    2024-04-30T15:10:09.0633333+00:00

    Without writing the entire script, here's one way of getting the management chain for a single user:

    $username = <sAMAccountname>
    $user = Get-ADUser -Identity $username -Properties manager,DisplayName
    $m = @{DisplayName=$user.DisplayName; Managers=@()}
    while ($user.manager) {
        $manager = Get-ADUser -Identity $user.manager -Properties Manager,DisplayName
        $m.'Managers' += $manager.DisplayName
        $user = $manager
    }
    [PSCustomObject]@{User=$m.DisplayName;Managers=($m.managers -join'|')}
    

    Note that the code ASSUMES that each user has only a single manager! The AD property 'managedBy' is a multi-valued property. If all your users have only one manager it should work. If there are multiple managers you'll have to modify the code.


1 additional answer

Sort by: Most helpful
  1. Michael-5966 20 Reputation points
    2024-05-01T12:33:04.5733333+00:00

    What i have tried was this:

    $username = <sAMAccountname>

    $user = Get-ADUser -Identity $username -Properties manager

    [array]$managementChain = @()

    while ($user.manager) {

    $manager = Get-ADUser -Identity $user.manager -Properties DisplayName 
    
    $managementChain += [PSCustomObject]@{
    
        "Employee DisplayName" = $($user.DisplayName)
    
        "Manager DisplayName"  = $($manager.DisplayName)
    
    }
    
    $user = $manager
    
    $managementChain | Export-Csv -path
    

    }

    All I get here is a export of a csv with 2 columns first is EmployeeDisplayName: but no info there. Second column is Manager DisplayName: I only get the direct manager display name. I'm sure I'm doing something wrong here.