The Invoke-MgGraphRequest
cmdlet is a powerful tool in PowerShell for sending custom requests directly to the Microsoft Graph API. While there are specialized cmdlets for common tasks like updating users (e.g. Update-MgUser
), sometimes using Invoke-MgGraphRequest
offers greater flexibility, especially when you need more control over API requests or perform tasks that are not covered by existing cmdlets.
This article focuses on how to use Invoke-MgGraphRequest
specifically for updating user properties in Microsoft 365 through the Graph API. We will cover syntax, usage examples, common errors and solutions, and use cases where this cmdlet shines.
Invoke-MgGraphRequest -Method PATCH -Uri "https://graph.microsoft.com/v1.0/users/$userId" -Body <JSON body> -ContentType "application/json"
Where:
https://graph.microsoft.com/v1.0/users/{user-id}
).# Define the user ID or UPN (User Principal Name)
$userId = "85fcdac1-5016-4bc3-b311-ad06fd410839"
# Define the update payload here, updating the display name
$body = @{
displayName = "Sir Ian Botham"
} | ConvertTo-Json
# Perform the PATCH request to update the user's display name
Invoke-MgGraphRequest -Method PATCH -Uri "https://graph.microsoft.com/v1.0/users/$userId" -Body $body -ContentType "application/json"
This example updates the display name of the user identified by $userId
to “Sir Ian Botham”.
$userId = "85fcdac1-5016-4bc3-b311-ad06fd410839"
$body = @{
jobTitle = "Senior Manager"
department = "IT"
} | ConvertTo-Json
Invoke-MgGraphRequest -Method PATCH -Uri "https://graph.microsoft.com/v1.0/users/$userId" -Body $body -ContentType "application/json"
This updates the user’s job title to "Senior Manager" and department to "IT".
$userId = "85fcdac1-5016-4bc3-b311-ad06fd410839"
$body = @{
mobilePhone = "+1 555 123 4567"
} | ConvertTo-Json
Invoke-MgGraphRequest -Method PATCH -Uri "https://graph.microsoft.com/v1.0/users/$userId" -Body $body -ContentType "application/json"
User.ReadWrite.All
).ConvertTo-Json
to ensure proper formatting.https://graph.microsoft.com/v1.0/users/{user-id}
) to avoid errors.Update-MgUser
can handle many user updates, Invoke-MgGraphRequest
allows for more flexibility, especially when working with properties not easily handled by other cmdlets.Invoke-MgGraphRequest
can be a lifesaver.Cause: Lack of proper permissions
Solution: Ensure you have User.ReadWrite.All
or Directory.ReadWrite.All
permissions in Azure AD.
Cause: Invalid request body or URL
Solution: Check the payload JSON formatting and verify the URL is correct (e.g. use the full API URL).
Cause: User ID is incorrect or user does not exist
Solution: Double-check the $userId
variable to ensure it points to a valid user.
Cause: API throttling due to hitting rate limits
Solution: Implement retry logic with exponential backoff, especially when dealing with bulk updates.
While specialized PowerShell cmdlets like Update-MgUser
offer convenience, Invoke-MgGraphRequest
provides a flexible, powerful way to interact with the Microsoft Graph API for tasks that require customizations or access to advanced features. Whether you need to update a user's display name, department, or job title, Invoke-MgGraphRequest
allows you to accomplish this with ease.
In cases where Graph PowerShell cmdlets fall short, this generic request cmdlet provides a way to perform any operation supported by the Graph API, making it an essential tool for administrators. By leveraging this flexibility, IT admins can address a variety of scenarios from bulk updates to custom user property management.
© m365corner.com. All Rights Reserved. Design by HTML Codex