This guide explores how to use Update-MgUser cmdlet in Graph PowerShell to modify user attributes in your Microsoft 365 tenant. Learn how to update properties such as display name, phone number, and department, and explore examples for both single and bulk user updates.
Updating user information in Microsoft 365 is a common administrative task. The Update-MgUser cmdlet in the Microsoft Graph PowerShell module provides a powerful way to update user properties. This article will cover the Update-MgUser cmdlet basics like the cmdlet prerequisites, cmdlet syntax with parameter explanations, various usage examples, helpful tips, common errors involved with solutions.
Install-Module -Name Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.ReadWrite.All"
Update-MgUser -UserId <String> [-AccountEnabled <Boolean>] [-DisplayName <String>] [-JobTitle <String>] [-MobilePhone <String>] [-OfficeLocation <String>] [-OtherMails <String[]>] [-PreferredLanguage <String>] [-Surname <String>] [-UserPrincipalName <String>] []
-UserId:
The unique identifier of the user to update. This can be the user's Id, UserPrincipalName, or Email.-AccountEnabled:
Specifies whether the user's account is enabled or disabled.-DisplayName:
The user's display name.-JobTitle:
The user's job title.-MobilePhone:
The user's mobile phone number.-OfficeLocation:
The user's office location.-OtherMails:
Additional email addresses for the user.-PreferredLanguage:
The user's preferred language.-Surname:
The user's last name.-UserPrincipalName:
The user's principal name (user's sign-in name).Update-MgUser -UserId "john.doe@contoso.com" -DisplayName "Johnathan Doe" -JobTitle "Senior Developer"
Update-MgUser -UserId "jane.smith@contoso.com" -AccountEnabled $true
Update-MgUser -UserId "alex.wang@contoso.com" -MobilePhone "+1 555 555 5555" -OfficeLocation "Building 4" -PreferredLanguage "en-US"
Update-MgUser -UserId "mary.jones@contoso.com" -OtherMails @("mary.jones@otherdomain.com")
Update-MgUser -UserId "old.username@contoso.com" -UserPrincipalName "new.username@contoso.com"
Useful for updating physical location information for directory accuracy or compliance.
Update-MgUser -UserId "alex.wilson@yourdomain.com" -BodyParameter @{
city = "Seattle"
country = "United States"
}
CSV File Example:
UserPrincipalName,JobTitle,Department,OfficeLocation
johndoe@domain.com,Manager,Sales,New York
janedoe@domain.com,Engineer,IT,San Francisco
$users = Import-Csv -Path "C:\Path\To\Users.csv"
foreach ($user in $users) {
$userParams = @{
JobTitle = $user.JobTitle
Department = $user.Department
OfficeLocation = $user.OfficeLocation
}
Update-MgUser -UserId $user.UserPrincipalName -BodyParameter $userParams
}
Error Message | Solution |
Insufficient Permissions Error: You might encounter an error indicating insufficient permissions. | Ensure your account has the User.ReadWrite.All permission. Reconnect with the required scopes if necessary.
|
Invalid UserId Error: Specifying an incorrect or non-existent UserId will result in an error. | Verify UserId exists using Get-MgUser cmdlet before performing the update operation. |
Incorrect Parameter Values Error: Providing invalid values for parameters (e.g., improperly formatted email addresses). | Ensure all parameter values are correctly formatted and valid. Double-check the input data. |
What is Update-MgUser used for?
Update-MgUser is a Microsoft Graph PowerShell cmdlet used to modify user attributes in a Microsoft 365 tenant. It allows updating various properties, such as display name, department, job title, and phone numbers.
How can I update a user’s display name using Update-MgUser?
To update a user’s display name, use the following script:
Update-MgUser -UserId "<UserPrincipalName>" -BodyParameter @{ "displayName" = "New Display Name" }
Can I update multiple users at once using Update-MgUser?
Yes, bulk updates can be performed using a CSV file. Prepare the file with the following format:
UserPrincipalName,DisplayName,Department
user1@domain.com,New Display Name 1,Sales
user2@domain.com,New Display Name 2,Marketing
Use this script to update user properties:
$Users = Import-Csv -Path "C:\Path\To\File.csv"
foreach ($User in $Users) {
$Body = @{
"displayName" = $User.DisplayName
"department" = $User.Department
}
Update-MgUser -UserId $User.UserPrincipalName -BodyParameter $Body
}
Can I update multiple properties of a user simultaneously using Update-MgUser?
You can update multiple user properties in a single call by passing them together in the -BodyParameter hashtable. For example:
$params = @{
displayName = "Alex Morgan"
jobTitle = "Senior Analyst"
department = "Finance"
officeLocation = "Building 5"
}
Update-MgUser -UserId "alex.morgan@domain.com" -BodyParameter $params
What should I do if I get a "Request_BadRequest" error while using Update-MgUser?
This error usually means you're trying to update an unsupported or improperly formatted property.
Double-check
try {
Update-MgUser -UserId "user@domain.com" -BodyParameter $params
} catch {
$_.Exception.Message
}
Update-MgUser
cmdlet can only modify certain properties of a user object. Attempting to update read-only or system-managed fields (like UserPrincipalName
for synced users) will result in errors.
Update-MgUser
will fail with a Property is read-only
error.The Update-MgUser cmdlet is a versatile tool for updating user information in Microsoft 365. By understanding its parameters and usage, you can efficiently manage user properties. Remember to handle errors gracefully and verify your input data to avoid common pitfalls. With these practices, you can ensure smooth and effective updates to your organization's user information.
Suggested Reading
© m365corner.com. All Rights Reserved. Design by HTML Codex