Ultimate Guide for Using Get-MgUserManager Cmdlet

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.

Who Are Microsoft 365 User Managers?

In Microsoft 365, a user manager represents the person to whom a user directly reports. This relationship is used to:

  • Create approval workflows in tools like Power Automate.
  • Provide managers with insights into their team’s activities.
  • Ensure organizational hierarchy is accurately reflected for compliance and reporting purposes.

Having an accurate manager-user relationship is critical for ensuring smooth operations across HR, IT, and management workflows.

Why Use Get-MgUserManager?

The Get-MgUserManager cmdlet provides an efficient way to programmatically retrieve the manager details for one or more users. Key advantages include:

  • Automation: Streamline tasks like auditing manager assignments or creating reports.
  • Accuracy: Quickly identify discrepancies in reporting structures.
  • Integration: Use manager details in automated workflows, approvals, and HR processes.

Setting Up Microsoft Graph PowerShell

To use Get-MgUserManager, you need to set up Microsoft Graph PowerShell:

  1. Install the Module: Install-Module Microsoft.Graph -Scope CurrentUser
  2. Connect to Microsoft Graph: Connect-MgGraph -Scopes "User.Read.All"
  3. Disconnect After Use: Disconnect-MgGraph

Exploring the Get-MgUserManager Cmdlet

The Get-MgUserManager cmdlet retrieves the manager information for a specific user. It simplifies tasks like retrieving reporting structures and generating team-based reports.

Cmdlet Syntax

Get-MgUserManager -UserId <String> [CommonParameters]

Usage Examples

1. Retrieve the Manager Information for a Specific User

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:

  • Id: The manager’s unique identifier.
  • DisplayName: The manager’s full name.
  • UserPrincipalName: The manager’s email address.
  • Mail: The manager’s primary email address.

2. Retrieve Manager Information for Multiple Users

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:

  • User ID
  • Manager ID
  • Manager’s name
  • Manager’s email address

Best Practices for Using Get-MgUserManager

  • Validate User Accounts: Ensure that the user accounts are active and valid using the Get-MgUser cmdlet.
  • Automate Reporting: Use Get-MgUserManager in scripts to generate automated reports on organizational hierarchy.
  • Handle Errors Gracefully: Use try-catch blocks to handle users without managers.
  • try {  
        $managerId = (Get-MgUserManager -UserId $userId).Id  
    } catch {  
        Write-Output "Manager not found for $userId"  
    }  
                                    
  • Use -WhatIf for Testing: Preview commands before execution to ensure they perform as expected.
  • Get-MgUserManager -UserId "user@domain.com" -WhatIf

Conclusion

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!


Permission Required

Example:


                            


                            


                            

© M365Corner. All Rights Reserved.