How to Create a Report of Managers and Their Direct Reports from Azure AD

Pursuing Accuracy in Tenant Directories

Last November, I wrote about why it’s important to have an accurate tenant directory (Azure AD). That article includes a script to check accounts for missing properties, one of which being the Manager. If that property isn’t populated, it means that an account is not listed as a direct report of another account (the manager), which makes it difficult for features like the Teams organizational view or Microsoft 365 profile card to work properly.

The Need to Avoid a Decaying Azure AD

Unless Azure AD receives updates from a HR system, it’s quite unsurprising when the directory loses track of manager-direct report relationships over time. And if no one checks, the directory will gradually decay to a point where its view of the organization’s structure is worthless. All of which means that we should check what’s in the directory periodically.

I have no idea about how individual companies record management structures, so I can’t help by telling you how to link systems with Azure AD. What I can do is show how easy it is to generate a report of what’s in Azure AD which can be used to identify potential issues to fix.

Some Azure AD Cmdlets Might Help

The idea is to find all the accounts in the tenant which have direct reports. It’s easy enough to do this for a single user. The Get-AzureADUserDirectReport cmdlet exists for this purpose. Once you know the object identifier for an Azure AD account, you can run:

Get-AzureADUserDirectReport -ObjectId eff4cd58-1bb8-4899-94de-795f656b4a18 | ft displayname

DisplayName
-----------
Kim Akers
Jeff Guillet
Ben Owens (Business Director)
Ståle Hansen (Office 365 for IT Pros)
James Ryan
Vasil Michev (Technical Guru)

The Get-AzureADUserManager cmdlet finds the manager of an account.

Get-AzureADUserManager -ObjectId cad05ccf-a359-4ac7-89e0-1e33bf37579e | ft DisplayName

DisplayName
-----------
Tony Redmond

It’s therefore possible to loop down through each user to find out who is their manager with code like this:

$Users = Get-AzureADUser -All:$true
# Now get rid of all the accounts created for room and resource mailboxes, service accounts etc.
$Users = $Users |?{($_.UserType -eq "Member" -and $_.AssignedLicenses -ne $Null)}

ForEach ($User in $Users) {
   $Manager = Get-AzureADUserManager -ObjectId $User.ObjectId
   If ($Manager) {
      Write-Host $User.DisplayName "manager is" $Manager.DisplayName }
   Else {
      Write-Host "No manager found for" $User.DisplayName }
}

Although this approach works, it means that we must find all users and then figure out who their manager is before reporting. A more logical approach is to find the managers in the organization and then work out their direct reports. There’s no straightforward way to do that with the Azure AD cmdlets without using intermediate arrays to store and then analyze who reports to who, but an easier way exists using the Exchange Get-User cmdlet.

Using Get-User to Retrieve Manager and Direct Reports

The Get-User cmdlet (part of the Exchange Online Management module) retrieves information about an account. The trick is that it can run a server-side filter to retrieve the direct reports of a manager. Better again, the cmdlet can also tell us which accounts have direct reports (the managers), including those who have no current direct reports.

To find the managers, we can run:

[array]$Managers = Get-User -Filter {DirectReports -ne $null} | Select DisplayName, UserPrincipalName, ExternalDirectoryObjectId, DistinguishedName

Once we know the managers, it’s simple to loop through each account to find their direct reports. The trick here is to filter using the distinguished name attribute for a manager’s account. It’s the same technique as used when Get-Recipient finds the set of groups a user belongs to. The technique is exploited in this article about generating a report of membership of Teams and Microsoft 365 Groups. Here’s how to find the set of direct reports for a manager:

[array]$Reports = Get-User -Filter "Manager -eq '$Dn'"

Once we know how to find managers and their direct reports, it’s easy to turn the data into a report, which is what I’ve done in a PowerShell script, which you can download from GitHub. Figure 1 shows the HTML report created by the script. Feel free to customize the report to your heart’s content.

The Managers and Direct Reports Listing created from Azure AD
Figure 1: The Managers and Direct Reports Listing created from Azure AD

PowerShell tip: We declare variables used to receive query results as arrays to make sure that we get an accurate count if only one item is returned. If you leave PowerShell to decide, when a single item is found by a query, the result is that item. You won’t be able to find the count for the item because it’s not an array or list. But if you explicitly declare the variable as an array, PowerShell respects your choice and you’ll be able to get a count even if only one item is in the array.


The Office 365 for IT Pros eBook is packed full of useful information, tips, and suggestions. That’s why the book is over 1,250 pages long… So much knowledge, so little time to read it all!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.