Managing user attributes in Microsoft 365 can be a time-consuming task, especially when you need to update multiple users. With Graph PowerShell module, you can automate this process and update user attributes in bulk using a CSV file. This article provides a step-by-step guide on how to achieve this.
Install-Module -Name Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.ReadWrite.All"
UserPrincipalName,DisplayName,JobTitle,Department,MobilePhone
user1@domain.com,John Doe,Manager,HR,1234567890
user2@domain.com,Jane Smith,Engineer,IT,0987654321
# 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 {
# Prepare the update payload
$updateParams = @{
DisplayName = $user.DisplayName
JobTitle = $user.JobTitle
Department = $user.Department
MobilePhone = $user.MobilePhone
}
# Update the user attributes
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
The script does the following:
The script can be enhanced by doing the following:
You can update any writable user attributes such as Department, JobTitle, OfficeLocation, MobilePhone
, and more. Ensure the attributes are supported and modifiable through Microsoft Graph.
The script will typically throw an error for that specific user, log it (if logging is implemented), and continue processing the rest. It’s recommended to include error handling to skip failed records gracefully.
Yes, you can update multiple properties by including them in the -BodyParameter
hashtable during the Update-MgUser
call. For example, both Department
and JobTitle
can be updated in a single request.
Yes, you must have delegated or app permissions such as User.ReadWrite.All
or Directory.AccessAsUser.All
to update user profiles using Graph PowerShell.
-BodyParameter
for Clean and Scalable Updates-BodyParameter
parameter.jobTitle
, department
, or officeLocation
.Automating the bulk update of user attributes using the Graph PowerShell module can save a significant amount of time and effort. By leveraging a simple CSV file and a PowerShell script, you can efficiently manage user attributes across your organization.
© m365corner.com. All Rights Reserved. Design by HTML Codex