Microsoft Graph PowerShell provides administrators with robust cmdlets to manage user profiles in Microsoft 365. Two cmdlets, Get-MgUser
and Update-MgUser
, are often used together to retrieve user properties and update them efficiently. In this article, we’ll walk through the usage of these cmdlets together, practical use cases, troubleshooting tips, and best practices.
Managing user accounts is a core responsibility for Microsoft 365 administrators. The Get-MgUser
cmdlet retrieves detailed information about a user or group of users, while the Update-MgUser
cmdlet updates user properties such as job titles, phone numbers, or department details. Pairing these cmdlets simplifies user management workflows and enhances productivity.
# Step 1: Retrieve the user details
$userId = "john.doe@domain.com" # Replace with the UPN or ObjectId of the user
$user = Get-MgUser -UserId $userId
# Display current user properties
Write-Output "Current User Profile:"
Write-Output "Display Name: $($user.DisplayName)"
Write-Output "Job Title: $($user.JobTitle)"
Write-Output "Department: $($user.Department)"
Write-Output "`n"
# Step 2: Update the user's job title and department
$updatedProperties = @{
JobTitle = "Senior Developer"
Department = "Engineering"
}
Update-MgUser -UserId $userId -BodyParameter $updatedProperties
# Step 3: Verify the changes
$updatedUser = Get-MgUser -UserId $userId
Write-Output "Updated User Profile:"
Write-Output "Display Name: $($updatedUser.DisplayName)"
Write-Output "Job Title: $($updatedUser.JobTitle)"
Write-Output "Department: $($updatedUser.Department)"
User.ReadWrite.All
for updating user profiles and User.Read.All
for retrieving details.$users = Get-MgUser -Filter "department eq 'Sales'"
foreach ($user in $users) {
Update-MgUser -UserId $user.Id -BodyParameter @{ Department = "New Sales" }
}
Error | Cause | Solution |
Access Denied | Insufficient permissions for the operation | Assign the required API permissions (User.ReadWrite.All) in Azure AD. |
Resource Not Found | Invalid or non-existent -UserId specified | Verify the UserId is correct (use UPN or ObjectId). |
Property not supported for update | Attempted to update a read-only property | Review Microsoft Graph API documentation to confirm writable properties. |
API Throttling | Too many requests sent in a short time | Implement retry logic with exponential backoff. |
Invalid JSON in -BodyParameter | Malformed payload in the -BodyParameter argument | Ensure JSON object adheres to the Graph schema and avoid extra commas. |
Pairing Get-MgUser and Update-MgUser streamlines user management in Microsoft 365. Whether updating individual profiles or performing bulk updates, these cmdlets provide flexibility and control. By following best practices and handling potential errors, you can create efficient scripts for routine administrative tasks.
Start integrating these cmdlets into your workflows today and experience the power of Microsoft Graph PowerShell for managing user profiles effectively!
© m365corner.com. All Rights Reserved. Design by HTML Codex