How to Use Set-MgUserManagerByRef to Assign Microsoft 365 User Manager?

Managing reporting relationships in Microsoft 365 is essential for smooth organizational workflows. Whether you're setting up organizational charts, configuring approval hierarchies, or syncing with HR systems, assigning a manager to a user is a vital step. In this blog, we'll walk you through how to use the Set-MgUserManagerByRef cmdlet from Microsoft Graph PowerShell to assign managers efficiently.

What is Set-MgUserManagerByRef?

Set-MgUserManagerByRef is a Microsoft Graph PowerShell cmdlet that allows administrators to assign or update a manager for a user in Microsoft 365. The relationship is established by referencing the manager’s unique ID (GUID) or User Principal Name (UPN) through an @odata.id URL structure.

This cmdlet simplifies the process of setting managerial hierarchies programmatically across your tenant.

Why Use Set-MgUserManagerByRef?

Here are a few reasons why this cmdlet is widely used by IT admins:

  • Automates manager assignment during onboarding
  • Updates reporting lines during organizational restructuring
  • Prepares accurate org charts and approval workflows
  • Supports bulk operations using CSV files
  • Integrates seamlessly with other Graph API-based automation tools

Cmdlet Syntax

Set-MgUserManagerByRef -UserId  -BodyParameter <Object>[<CommonParameters>]

Parameters:

  • -UserId: The user’s UPN or Object ID to whom the manager will be assigned.
  • -BodyParameter: A hashtable that includes the manager's @odata.id.

Usage Examples

Assign a Manager to a Single User

Here’s how you can assign a manager to a single user using the Graph PowerShell module:

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

📝 Note: Replace the @odata.id value with the actual Object ID of the manager.

Assign Managers in Bulk Using a CSV File

For large organizations, bulk updates are much more efficient. Here's how you can automate manager assignments using a CSV file.

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  
}
                                        

📌 Make sure both the user and manager exist and that you have the necessary permissions to perform this operation.

Frequently Asked Questions

  • Do I need any special permissions to use this cmdlet?
  • Yes. You need User.ReadWrite.All and Directory.ReadWrite.All permissions with administrative consent.

  • Can I use UPN instead of Object ID for the manager?
  • No. The @odata.id must reference the manager's Object ID via the Graph endpoint URL.

  • What happens if the manager ID is invalid?
  • You’ll receive a ResourceNotFound or BadRequest error. Always validate IDs before running bulk operations.

  • Can I remove a manager assignment using this cmdlet?
  • No. To remove a manager, use the Remove-MgUserManager cmdlet instead.

Use Cases

  • New Employee Setup: Automatically assign a manager during onboarding.
  • Department Changes: Quickly update reporting structures during reorgs.
  • Org Charts: Maintain accurate managerial links for visualization tools.
  • Workflow Approvals: Enable dynamic manager-based approval systems in Power Automate or similar platforms.

Conclusion

The Set-MgUserManagerByRef cmdlet is a powerful and flexible tool that simplifies the assignment of managers in Microsoft 365. Whether you're working with a single user or handling bulk updates, this cmdlet can help you automate and streamline your administrative processes. Just make sure to use the correct @odata.id format and verify permissions before executing any changes.

💡 Start small with a single user test before scaling your script across the organization.

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