This guide demonstrates how to use the Invoke-MgGraphRequest cmdlet in Microsoft Graph PowerShell to update user details in Microsoft 365. Learn how to modify properties like job title, department, and mobile phone number with practical examples.
Updating a user in Microsoft 365 using Graph PowerShell often involves specific cmdlets like Update-MgUser. However, there are scenarios where you may need to use a more flexible approach, such as Invoke-MgGraphRequest, which allows you to send custom Graph API calls directly. This article will guide you through using Invoke-MgGraphRequest to update a user, offering practical examples, tips, common errors, and real-world use cases.
Invoke-MgGraphRequest -Method PATCH -Uri "https://graph.microsoft.com/v1.0/users/{id}" -Body @{
passwordProfile = @{
forceChangePasswordNextSignIn = $true
password = "NewPassword123"
}
}
The key parameters are:
PATCH for updating.https://graph.microsoft.com/v1.0/users/{id}).$uri = "https://graph.microsoft.com/v1.0/users/sam.user@contoso.com"
$body = @{
passwordProfile = @{
forceChangePasswordNextSignIn = $false
password = "SecurePassw0rd!"
}
}
Invoke-MgGraphRequest -Method PATCH -Uri $uri -Body $body
In this example, the user sam.user@contoso.com will have their password updated to SecurePassw0rd! and won’t be required to change their password at the next sign-in.
$users = @("user1@contoso.com", "user2@contoso.com", "user3@contoso.com")
foreach ($user in $users) {
$uri = "https://graph.microsoft.com/v1.0/users/$user"
$body = @{
officeLocation = "New York Office"
}
Invoke-MgGraphRequest -Method PATCH -Uri $uri -Body $body
}
This script updates the officeLocation attribute for each user in the list to "New York Office".
$users = Import-Csv -Path "C:\Users.csv"
foreach ($user in $users) {
$uri = "https://graph.microsoft.com/v1.0/users/$($user.UserPrincipalName)"
$body = @{
displayName = $user.DisplayName
jobTitle = $user.JobTitle
department = $user.Department
mobilePhone = $user.MobilePhone
}
Invoke-MgGraphRequest -Method PATCH -Uri $uri -Body $body
}
This example uses a CSV file to update multiple user properties (DisplayName, JobTitle, Department, and MobilePhone) in bulk.
Invoke-MgGraphRequest (e.g., https://graph.microsoft.com/v1.0/users/{id}).User.ReadWrite.All) to update users via Microsoft Graph API.| Error | Cause | Solution |
| 400 Bad Request | The request body contains invalid or incorrect values. | Double-check the properties and format of your body parameters. Ensure that you're sending the correct data type (e.g., Boolean for forceChangePasswordNextSignIn). |
| 403 Forbidden | Insufficient permissions to perform the operation. | Ensure that the account running the script has the necessary Graph API permissions. You may need to request admin consent for User.ReadWrite.All. |
| 404 Not Found | The user could not be found based on the provided ID or UPN. | Verify the UserPrincipalName or ID used in the API URL. Ensure that the user exists and that you have access to manage their details. |
Invoke-MgGraphRequest to reset the passwords for a large set of users across multiple departments. This can be useful in security breaches where resetting credentials quickly is a priority.displayName or jobTitle, you can script the process using Invoke-MgGraphRequest, saving time and ensuring consistency across the organization.1. What is Invoke-MgGraphRequest used for?
Invoke-MgGraphRequest is a Microsoft Graph PowerShell cmdlet used to make custom API calls, allowing updates to user properties in Microsoft 365 beyond the standard cmdlets.
2. How can I verify that a user's details were updated?
Use the Get-MgUser cmdlet to confirm the changes:
Get-MgUser -UserId "<UserPrincipalName>" | Select-Object DisplayName, JobTitle, Department
3. What permissions are required to update user properties?
You need the User.ReadWrite.All or Directory.ReadWrite.All permission in Microsoft Graph PowerShell. Ensure these permissions are granted in Azure AD.
While Microsoft Graph PowerShell offers specific cmdlets for most user-related tasks, Invoke-MgGraphRequest is a powerful and flexible alternative when you need to perform custom operations or when cmdlets fall short. By leveraging direct API calls, you can automate updates for single users, multiple users, or even bulk changes via CSV, significantly streamlining your administrative workflows. Ensure that you use proper error handling and testing, and don’t forget to verify your API permissions to avoid common issues.
© m365corner.com. All Rights Reserved. Design by HTML Codex