This guide explains how to use the Set-MgUserManagerByRef cmdlet in Microsoft Graph PowerShell to assign or update a manager for a user. Learn how to link a user to a manager with practical examples for single and bulk operations
Managing user relationships is crucial in Microsoft 365 (M365) environments. Assigning a manager to a user account can streamline workflows, enhance reporting, and improve organizational hierarchy tracking. The Set-MgUserManagerByRef cmdlet in Microsoft Graph PowerShell allows administrators to set or update the manager for a user. This article will guide you through the cmdlet's syntax, provide real-world usage examples, offer tips, outline common errors with solutions, and conclude with practical use cases.
Set-MgUserManagerByRef -UserId <String> -BodyParameter <Hashtable>
This example assigns a manager to a single user by specifying the UserId and manager-id.
$Manager = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/12345678-90ab-cdef-ghij-klmnopqrstuv"
}
Set-MgUserManagerByRef -UserId "john.doe@company.com" -BodyParameter $Manager
In this scenario, we loop through multiple users and assign a manager using the Set-MgUserManagerByRef cmdlet.
$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 example shows how to assign managers to users by importing the user details from a CSV file. The CSV should contain columns for UserPrincipalName and ManagerId.
CSV File Format
UserPrincipalName,ManagerId
user1@company.com,manager1-id
user2@company.com,manager2-id
$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
}
Cause: This error occurs when the manager’s UserId is invalid or formatted incorrectly in the hashtable.
Solution: Double-check the manager-id in the @odata.id field to ensure it points to the correct directory object. Confirm the manager exists in your tenant.
Cause: This occurs when the UserId provided for the target user is incorrect.
Solution: Ensure that the user principal name or user ID is correctly specified in the -UserId parameter. Use Get-MgUser to verify the user exists.
1. What is Set-MgUserManagerByRef used for?
Set-MgUserManagerByRef is a Microsoft Graph PowerShell cmdlet used to assign or update the manager property for a user in Azure AD.
2. How can I assign a manager to a user?
Use the following script to set a manager for a user:
$Body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/<ManagerObjectId>"
}
Set-MgUserManagerByRef -UserId "<UserObjectId>" -BodyParameter $Body
3. How can I verify if a manager is set for a user?
Use the Get-MgUserManager cmdlet to verify the manager:
Get-MgUserManager -UserId "<UserObjectId>"
4. What permissions are required to use Set-MgUserManagerByRef?
You need the User.ReadWrite.All or Directory.ReadWrite.All permission in Microsoft Graph PowerShell. Ensure appropriate permissions are granted in Azure AD.
The Set-MgUserManagerByRef cmdlet is a powerful tool for managing user-manager relationships in Microsoft 365. By properly formatting the BodyParameter and utilizing CSV imports for bulk assignments, administrators can efficiently manage organizational hierarchies. Whether you are setting managers for a few users or updating relationships across the organization, this cmdlet helps simplify the process.
© m365corner.com. All Rights Reserved. Design by HTML Codex