Skip to content

Update Azure AD users with Microsoft Graph PowerShell

Sometimes, you need to add or change a single or multiple Azure AD user information. We will show you how to update one property and multiple properties for a single Azure AD user. Also, we will bulk set multiple properties for all Azure AD users. In this article, you will learn how to update Azure AD users with Microsoft Graph PowerShell.

Set-AzureADUser and Set-MsolUser deprecated

Microsoft announced the Set-AzureADUser and Set-MsolUser cmdlets will be deprecated on March 30, 2024. You need to replace the MS Online PowerShell modules with Microsoft Graph PowerShell.

To update an Azure AD user property, we will use the Microsoft Graph PowerShell cmdlets.

Manage Azure AD users with Microsoft Graph PowerShell

We created specific articles to manage Azure AD users with Microsoft Graph PowerShell:

Connect to Microsoft Graph PowerShell

Before you start, you must install the Microsoft Graph PowerShell module, including the Microsoft Graph Beta module.

Run the below command to install the Microsoft Graph module.

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

Important: Always install the Microsoft Graph PowerShell and Microsoft Graph Beta PowerShell modules. That’s because some cmdlets are not yet available in the final version and will not work. Update both modules to the latest version before you run a cmdlet or script to prevent errors and incorrect results.

You also need to connect to MS Graph with the below scopes.

Connect-MgGraph -Scopes "User.ReadWrite.All"

Now, you can use the Update-MgUser and Update-MgBetaUser cmdlets with Microsoft Graph PowerShell.

Note: Not all user account properties can be updated by member or guest users with their default permissions without administrator roles.

Update single Azure AD user properties

We will show how to change a single Azure AD user property with Microsoft Graph PowerShell. Then, we will show how to update multiple properties for a single Azure AD user.

Update-MgBetaUser Azure AD property

To update the Azure AD user account information, we will use the Update-MgBetaUser cmdlet.

You can add the country of a single user. In our example, the user Amanda Hansen has no country set in the information details.

Run the below PowerShell command example to add the user country.

Update-MgBetaUser -UserId "Amanda.Hansen@m365info.com" -Country "GB"

It automatically adds the country information for the Azure AD user.

Change single Azure AD user property

If a user changes to a different department, you might need to edit that for the user. We will use the -Department parameter to add the new information.

In our example, we want to change the from the Sales department to Marketing for the user, Amanda.Hansen@m365info.com.

Run the below PowerShell command example.

Update-MgBetaUser -UserId "Amanda.Hansen@m365info.com" -Department "Marketing"

It immediately updates the job information of the single Azure AD user.

Update multiple properties for a single Azure AD user

Let’s say you want to update the properties for a specific Azure AD user because this user has another job title and a new phone number.

In our example, we will use the -BusinessPhones parameter to add a number and the -JobTitle parameter to change the job for the user Amanda Hansen.

$UserId = (Get-MgUser -UserId "Amanda.Hansen@m365info.com").Id
Update-MgBetaUser -UserId $UserId -JobTitle "Marketing Manager" -BusinessPhone "+44 20 8885 6673"

To check that the user properties are updated correctly, we need to get the user details.

Get-MgBetaUser -UserId "Amanda.Hansen@m365info.com" | Select DisplayName, BusinessPhones, JobTitle, Mail, City, CompanyName, Department, EmployeeId, StreetAddress, Country

The PowerShell output results will show you added a phone number and changed the job title.

DisplayName    : Amanda Hansen
BusinessPhones : {+44 20 8885 6673}
JobTitle       : Marketing Manager
Mail           : Amanda.Hansen@m365info.com
City           : London
CompanyName    : NewCompany
Department     : Marketing
EmployeeId     : 123
StreetAddress  : X
Country        : GB

Update properties for multiple Azure AD users

We will change the property for multiple or all Azure AD users with a CSV file. We will also show you how to bulk set multiple properties for all Azure AD users from a CSV file.

Update job title for multiple Azure AD users

We want to change and set the job title for multiple Azure AD users. First, create a new CSV file with all the Azure AD users you want to update.

Open Microsoft Excel and type the below data to change and set the job title:

  1. Type UserPrincipalName at the top of column A
  2. Type JobTitle at the top of column B
  3. List the Azure AD users under column A
  4. List the new Job Titles for each user under column B

The CSV file should look like the below example.

Update Azure AD users property jobtitle from CSV with Microsoft Graph PowerShell
  1. Create a temp folder on the (C:) drive
  2. Name the file UsersJob.csv
  3. Save as type CSV UTF-8 (Comma delimited)(*.csv)
  4. Click Save
Update Azure AD users property jobtitle from CSV file

Note: It doesn’t matter if there are values typed or the values are empty (null) because everything from the CSV will overwrite it.

  1. Run the below PowerShell script
# Connect to Microsoft Graph
Connect-MgGraph -Scopes User.ReadWrite.All

# Read the CSV file
$users = Import-Csv -Path "C:\temp\UsersJob.csv"

# Go through each user in the CSV and update the job title
foreach ($user in $users) {
    $userPrincipalName = $user.UserPrincipalName
    $jobTitle = $user.JobTitle

    # Check if the user exists
    $existingUser = Get-MgBetaUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

    if ($existingUser) {
        # Check if the existing job title matches the new value
        if ($existingUser.JobTitle -eq $jobTitle) {
            # Job title already set with the same value
            Write-Host "User '$userPrincipalName' already has job title '$jobTitle'." -ForegroundColor Cyan
        }
        else {
            # Update the job title
            Update-MgBetaUser -UserId $userPrincipalName -JobTitle $jobTitle
            Write-Host "User '$userPrincipalName' updated job title to '$jobTitle' successfully." -ForegroundColor Green
        }
    }
    else {
        # User not found
        Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
    }
}

The PowerShell output results show the following:

  • Azure AD user updated new job title
  • Azure AD user already has set the same job title
  • Unknown user which can’t be found in Azure AD
User 'Brenda.Smith@m365info.com' updated job title to 'Manager' successfully.
User 'David.Kent@m365info.com' already has job title 'Trainee'.
User 'Julia.Wood@m365info.com' updated job title to 'Chef' successfully.
User 'Rene.Gibs@m365info.com' updated job title to 'Team Leader' successfully.
User 'Unknown@m365info.com' not found.

Update country for multiple Azure AD users

We want to change and set the country for multiple or all Azure AD users. First, create a new CSV file with all the Azure AD users you want to update.

Open Microsoft Excel and type the below data:

  1. Type UserPrincipalName at the top column A
  2. Type Country at the top of column B
  3. List the Azure AD users under column A
  4. List the new Country for each user under column B

See the below CSV file example.

Update-MgBetaUser property Country from CSV with Microsoft Graph PowerShell
  1. Create a temp folder on the (C:) drive
  2. Name the file UsersCountry.csv
  3. Save as type CSV UTF-8 (Comma delimited)(*.csv)
  4. Click Save
Update-MgBetaUser property Country from CSV file

Note: If the values for Country are empty (null) or there are values written for some users, then the new values of the CSV will replace them.

  1. Run the below PowerShell script
# Connect to Microsoft Graph
Connect-MgGraph -Scopes User.ReadWrite.All

# Read the CSV file
$users = Import-Csv -Path "C:\temp\UsersCountry.csv"

# Go through each user in the CSV and update the country
foreach ($user in $users) {
    $userPrincipalName = $user.UserPrincipalName
    $country = $user.Country

    # Check if the user exists
    $existingUser = Get-MgBetaUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

    if ($existingUser) {
        # Check if the existing country matches the new value
        if ($existingUser.Country -eq $country) {
            # Country already set with the same value
            Write-Host "User '$userPrincipalName' already has country '$country'." -ForegroundColor Cyan
        }
        else {
            # Update the country
            Update-MgBetaUser -UserId $userPrincipalName -Country $country
            Write-Host "User '$userPrincipalName' updated country to '$country' successfully." -ForegroundColor Green
        }
    }
    else {
        # User not found
        Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
    }
}

The PowerShell output results show the following:

  • Azure AD user updated new country
  • Azure AD user already has set the same country
  • Unknown user which can’t be found in Azure AD
User 'Brenda.Smith@m365info.com' updated country to 'US' successfully.
User 'David.Kent@m365info.com' already has country 'US'.
User 'Julia.Wood@m365info.com' updated country to 'Germany' successfully.
User 'Rene.Gibs@m365info.com' updated country to 'DK' successfully.
User 'Unknown@m365info.com' not found.

Bulk update Azure AD users multiple properties

Let’s say you want to change and set the Country, Employee ID, and Job Title for all the Azure AD users. You need to create a CSV file for all the users you want to update.

Note: An excellent approach is first to Export all Azure AD users and adjust the CSV file with the new properties for the users that you want to update. Once that is done, you can skip the below steps and go straight to the PowerShell script below.

Open Microsoft Excel and type the below data:

  1. Type UserPrincipalName at the top column A
  2. Type Country at the top of column B
  3. Type EmployeeId at the top of column C
  4. Type JobTitle at the top of column D
  5. List the Azure AD users under column A
  6. List the new Country for each user under column B
  7. List the new Employee IDs for each user under column C
  8. List the new Job titles for each user under column D

See the below CSV file example.

Bulk update Azure AD users multiple properties from CSV with Microsoft Graph PowerShell
  1. Create a temp folder on the (C:) drive
  2. Name the file UsersProperties.csv
  3. Save as type CSV UTF-8 (Comma delimited)(*.csv)
  4. Click Save
Bulk update Azure AD users multiple properties from CSV file

Note: If the values for the user properties are empty (null) or there are values written for some users, then the new values of the CSV will replace them.

  1. Run the below Powershell script
# Connect to Microsoft Graph
Connect-MgGraph -Scopes User.ReadWrite.All

# Read the CSV file
$users = Import-Csv -Path "C:\temp\UsersProperties.csv"

# Go through each user in the CSV and update the properties
foreach ($user in $users) {
    $userPrincipalName = $user.UserPrincipalName
    $employeeId = $user.EmployeeId
    $jobTitle = $user.JobTitle
    $country = $user.Country

    # Check if the user exists
    $existingUser = Get-MgBetaUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

    if ($existingUser) {
        # Check if the existing properties match the new values
        $updateNeeded = $false

        if ($existingUser.EmployeeId -ne $employeeId) {
            $existingUser.EmployeeId = $employeeId
            $updateNeeded = $true
        }

        if ($existingUser.JobTitle -ne $jobTitle) {
            $existingUser.JobTitle = $jobTitle
            $updateNeeded = $true
        }

        if ($existingUser.Country -ne $country) {
            $existingUser.Country = $country
            $updateNeeded = $true
        }

        if ($updateNeeded) {
            # Update the user properties
            Update-MgBetaUser -UserId $userPrincipalName -EmployeeId $employeeId -JobTitle $jobTitle -Country $country
            Write-Host "User '$userPrincipalName' updated successfully." -ForegroundColor Green
        }
        else {
            Write-Host "User '$userPrincipalName' properties are up to date." -ForegroundColor Cyan
        }
    }
    else {
        # User not found
        Write-Host "User '$userPrincipalName' not found." -ForegroundColor Red
    }
}

The PowerShell output results show the following:

  • Azure AD user updated new Country, EmployeeID, or Job title
  • Azure AD user already has these properties set
  • Unknown user which can’t be found in Azure AD
User 'Brenda.Smith@m365info.com' properties are up to date.
User 'David.Kent@m365info.com' updated successfully.
User 'Julia.Wood@m365info.com' updated successfully.
User 'Rene.Gibs@m365info.com' updated successfully.
User 'Unknown@m365info.com' not found.

You learned how to update Azure AD user properties with MS Graph PowerShell.

Read more: How to set Employee ID for Microsoft 365 users »

Conclusion

You learned how to update Azure AD users with Microsoft Graph PowerShell using the Update-MgUser cmdlet. You can change multiple properties for a single user with PowerShell. It’s also possible to bulk set properties for all Azure AD users with a CSV file.

Did you enjoy this article? You may also like Assign Microsoft 365 licenses with group-based licensing. Don’t forget to follow us and share this article.

o365info Team

o365info Team

This article was written by our team of experienced IT architects, consultants, and engineers.

This Post Has 7 Comments

  1. Hi,

    Thank you very much for the guide, super useful, especially when getting back to work with Azure after a pause.

    Having a problem though. We are running on-prem as well and everything is synced. After running the code, the following error occurs:
    User “user@domain.com” not found.

    Any ideas why this error could occur? Using the same piece of code with upn and jobTitle.
    Thanks!

  2. Thanks for your article it has been really useful.. but I have hit one problem…
    If the list I am importing has some mobile phone numbers on them, the command
    -MobilePhone does not like a blank answer
    The only way around it is to build a 2nd part to the script that will only try to update those that have a mobile number and ignore all those that do not, unless you have found another way round this?

    1. The Microsoft Graph PowerShell SDK (current version) does not support nulling the value. We will update the script when the Microsoft Graph PowerShell SDK supports the $null value. This should be out in V3.

      You can use the Invoke-GraphRequest for now.

      Here is an example:

      Invoke-GraphRequest -Method PATCH -Uri "https://graph.microsoft.com/v1.0/Users/Brenda.Smith@m365info.com" -Body '{"businessPhones": [],"mobilePhone": null}'

Leave a Reply

Your email address will not be published. Required fields are marked *