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.

Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 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.

    2 people found this answer helpful.

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.


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.