Bulk Update User Attributes Using Graph PowerShell

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.


Prerequisites

  • Install Microsoft.Graph Module: Install the Microsoft.Graph module if you haven't already. You can install it using the following command:
    Install-Module -Name Microsoft.Graph -Scope CurrentUser
  • User.ReadWrite.All Graph API Permission: Next, you need to connect with Microsoft Graph PowerShell module with the required permission (User.ReadWrite.All) by running the following command:
    Connect-MgGraph -Scopes "User.ReadWrite.All"
  • CSV File: Prepare a CSV file with the user attributes you want to update. The file should have headers corresponding to the attributes. Here's an example format:
    UserPrincipalName,DisplayName,JobTitle,Department,MobilePhone
    user1@domain.com,John Doe,Manager,HR,1234567890
    user2@domain.com,Jane Smith,Engineer,IT,0987654321

Bulk Update User Attributes Script

# 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

Script Explanation

The script does the following:

  • Connect to Microsoft Graph: It connects to Microsoft Graph with the required permissions to update user attributes.
  • Imports the CSV File: The CSV file containing user details is imported.
  • Loops Through Each User: For each user in the CSV file, the script prepares an update payload and uses the Update-MgUser cmdlet to update the user's attributes.
  • Handle Errors: Any errors that occur during the update process are caught and logged.
  • Disconnect from Microsoft Graph: Finally, the script disconnects from the Microsoft Graph session.

Further Enhancing the Script

The script can be enhanced by doing the following:

  • Adding Additional Attributes: Extend the script to update more attributes by adding more fields to the CSV file and updating the $updateParams hash table.
  • Logging: Implementing more robust logging to record successes and failures in a log file for auditing purposes.
  • Validation: Adding validation to check if the user exists before attempting to update their attributes.
  • Retry Mechanism: Implementing a retry mechanism for failed updates to handle transient errors or network issues.

💡 Use a Hashtable with -BodyParameter for Clean and Scalable Updates

When updating multiple user attributes, pass them as a hashtable using the -BodyParameter parameter.

This method is clean, script-friendly, and ideal for bulk updates driven by CSV input or automation logic.
⚠️ Ensure CSV Headers Match Graph Schema Property Names

When performing bulk updates using a CSV file, make sure your column headers match Microsoft Graph property names exactly — such as jobTitle, department, or officeLocation.

Mismatched headers will result in ignored fields or update failures.

Conclusion

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