Set-MgUserManagerByRef

What is Set-MgUserManagerByRef?

The Set-MgUserManagerByRef cmdlet in Microsoft Graph PowerShell is used to assign or update a manager for a user in Microsoft 365. Instead of directly modifying user attributes, it creates a reference between a user and their manager, aligning with organizational hierarchy management.


Why Use Set-MgUserManagerByRef?

This cmdlet is particularly useful when:

  • Assigning managers during onboarding.
  • Updating reporting structures in bulk.
  • Automating HR processes that require manager-user mappings.
  • Keeping Active Directory and Microsoft 365 organizational charts consistent.

By using PowerShell, administrators save time and reduce errors compared to manual updates in the Admin Center.


Prerequisites

Before running this cmdlet, connect to Microsoft Graph with the required permissions:

Connect-MgGraph -Scopes "User.ReadWrite.All"

How to Use Set-MgUserManagerByRef?

Syntax (essential parameters):

Set-MgUserManagerByRef -UserId <String> -BodyParameter <Hashtable>

You must provide the UserId (UPN or ObjectId) of the user and specify the manager reference using @odata.id.


Set-MgUserManagerByRef Examples

  • Example 1: Assign a Single Manager to a User
  • $Manager = @{
        "@odata.id" = "https://graph.microsoft.com/v1.0/users/12345678-90ab-cdef-ghij-klmnopqrstuv"
    }
    Set-MgUserManagerByRef -UserId "john.doe@company.com" -BodyParameter $Manager

    This assigns a manager with the given object ID to John Doe.

  • Example 2: Assign Managers to Multiple Users
  • $Manager = @{
        "@odata.id" = "https://graph.microsoft.com/v1.0/users/87654321-abcd-efgh-ijkl-mnopqrstuvwx"
    }
    $Users = @("user1@company.com", "user2@company.com", "user3@company.com")
                                                    
    foreach ($User in $Users) {
        Set-MgUserManagerByRef -UserId $User -BodyParameter $Manager
    }

    This loop assigns the same manager to multiple users at once.

  • Example 3: Assign Managers via CSV Import
  • CSV File Format:

    UserPrincipalName,ManagerId
    user1@company.com,manager1-id
    user2@company.com,manager2-id

    PowerShell Script:

    $CSVPath = "C:\Managers.csv"
    $Users = Import-Csv -Path $CSVPath
                                                    
    foreach ($User in $Users) {
    $Manager = @{
        "@odata.id" = "https://graph.microsoft.com/v1.0/users/$($User.ManagerId)"
    }
        Set-MgUserManagerByRef -UserId $User.UserPrincipalName -BodyParameter $Manager
    }

    This assigns managers to multiple users by reading from a CSV file, useful for bulk updates.


Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.

Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.

© Your Site Name. All Rights Reserved. Design by HTML Codex