Managing user hierarchies is essential in any organization. Knowing who reports to whom not only streamlines workflows but also ensures that approval processes, escalations, and communications are efficient.
The Get-MgUserManager cmdlet, part of the Microsoft Graph PowerShell module, allows administrators to retrieve manager information for users in Microsoft 365. This guide walks you through everything you need to know, including practical examples and best practices.
In Microsoft 365, a user manager represents the person to whom a user directly reports. This relationship is used to:
Having an accurate manager-user relationship is critical for ensuring smooth operations across HR, IT, and management workflows.
The Get-MgUserManager cmdlet provides an efficient way to programmatically retrieve the manager details for one or more users. Key advantages include:
To use Get-MgUserManager, you need to set up Microsoft Graph PowerShell:
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.Read.All"
Disconnect-MgGraph
The Get-MgUserManager cmdlet retrieves the manager information for a specific user. It simplifies tasks like retrieving reporting structures and generating team-based reports.
Get-MgUserManager -UserId <String> [CommonParameters]
To fetch the manager of a single user:
$managerId = (Get-MgUserManager -UserId "user@domain.com").Id $manager = Get-MgUser -UserId $managerId $manager | Select-Object Id, DisplayName, UserPrincipalName, Mail
This command retrieves the manager’s details, including:
To retrieve the manager details for all users in a specific department (e.g., Sales):
$users = Get-MgUser -Filter "Department eq 'Sales'" -All foreach ($user in $users) { $managerId = (Get-MgUserManager -UserId $user.Id).Id $manager = Get-MgUser -UserId $managerId [PSCustomObject]@{ UserId = $user.UserPrincipalName ManagerId = $manager.Id ManagerDisplayName = $manager.DisplayName ManagerUPN = $manager.UserPrincipalName ManagerMail = $manager.Mail } }
This script loops through all users in the Sales department, retrieves their manager details, and outputs:
try {
$managerId = (Get-MgUserManager -UserId $userId).Id
} catch {
Write-Output "Manager not found for $userId"
}
Get-MgUserManager -UserId "user@domain.com" -WhatIf
The Get-MgUserManager cmdlet is a powerful tool for Microsoft 365 administrators, enabling efficient retrieval of manager information. Whether you’re auditing reporting structures, generating organizational reports, or building automated workflows, this cmdlet simplifies the process and saves valuable time.
By following the best practices outlined in this guide, you can ensure accurate reporting structures and enhance your organization’s operational efficiency. Start using Get-MgUserManager today to streamline your administrative tasks and improve your organizational hierarchy!
© M365Corner. All Rights Reserved.