The Update-MgUser cmdlet in Microsoft Graph PowerShell is used to update attributes of Microsoft 365 user accounts. It supports modifying properties such as display names, job titles, office locations, phone numbers, languages, and more.
Updating user information is a common task for administrators. Automating it with Update-MgUser ensures:
Before running the cmdlet, install the Microsoft Graph module and connect with proper permissions:
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.ReadWrite.All"
Syntax (essential parameters):
Update-MgUser -UserId <UserPrincipalName or UserId> -Property
You can update a single property (like DisplayName) or pass a hashtable via -BodyParameter for multiple attributes in one command.
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"
Update-MgUser -UserId "alex.wilson@yourdomain.com" -BodyParameter @{
city = "Seattle"
country = "United States"
}
Update-MgUser -UserId "alex.johnson@yourdomain.com" -BodyParameter @{
officeLocation = "HQ-Block B"
}
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Path to the CSV file
$csvPath = "C:\Path\To\Your\CSV\users.csv"
# Import the CSV file
$users = Import-Csv -Path $csvPath
# Loop through each user and update attributes
foreach ($user in $users) {
try {
$updateParams = @{
DisplayName = $user.DisplayName
JobTitle = $user.JobTitle
Department = $user.Department
MobilePhone = $user.MobilePhone
}
Update-MgUser -UserId $user.UserPrincipalName -BodyParameter $updateParams
Write-Host "Successfully updated user: $($user.UserPrincipalName)"
}
catch {
Write-Host "Failed to update user: $($user.UserPrincipalName). Error: $_"
}
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex