How to Change UPN/Sign-In Name of Office 365 user using PowerShell

In Office 365 cloud world, users need to use their UPN (UserPrincipalName) as main login name to sign-in into any Office 365 apps. In some situations, we need to change the UPN for some users either to match the UPN with users’ primary email address or if users are created with UPN that ends-with .onmicrosoft.com ([email protected]).

In this post, I am going to share powershell script to modify userprincipalname of an user and update upn for bulk azure ad users from CSV. We can use Set-AzureADUser cmdlet to modify user properties and this cmdlet belongs to Azure AD V2 PowerShell module.

Note: Before proceed install Azure Active Directory PowerShell for Graph and run the below command to connect Azure AD V2 PowerShell module:

Connect-AzureAD

Rename Office 365 user/change user name part in UPN

You can run the following command to change the username part in required user’s UPN and you can also use the same commands to modify domain name of an user.

$old_upn= "[email protected]"
$new_upn= "[email protected]"
Set-AzureADUser -ObjectId $old_upn -UserPrincipalName $new_upn

Change UPN to match primary Email address for Bulk users from CSV

In many places, even though Office 365 service login UI asks email address, we should type the UPN of the user for successful login, unless the user’s login name (UserPrincipalName) and primary SMTP (Email address) match with each other. So to avoid confusion from end-users, we need to ensure UPN of an user should match with the user’s primary SMTP e-mail address.

You can use the below powershell script to update UPN of bulk users by importing users and their new upn (EmailAddress) from csv file.

Import-Csv 'C:\Office365Users.csv' | ForEach-Object {
$upn = $_."UserPrincipalName"
$newupn = $_."EmailAddress"
Write-Host "Changing UPN value from: "$upn" to: " $newupn -ForegroundColor Yellow
Set-AzureADUser -ObjectId $upn  -UserPrincipalName $newupn
}

Note: Your csv file (Office365Users.csv) should includes the column headers UserPrincipalName and EmailAddress (New UPN), if you have different headers you need to modify the above script accordingly.

Change domain name for bulk users

In some cases, after migrating users from On-Premise Active Directory using DirSync, new Office 365 users are created with Primary UPN that ends with domain part as .onmicrosoft.com (Ex: [email protected]). In this case, we can use the below script to modify upn with actual domain name.

$domain = "MTS.com"
Get-AzureADUser -All $True | Where { $_.UserPrincipalName.ToLower().EndsWith("onmicrosoft.com") } |
ForEach {
 $newupn = $_.UserPrincipalName.Split("@")[0] + "@" + $domain
 Write-Host "Changing UPN value from: "$_.UserPrincipalName" to: " $newupn -ForegroundColor Yellow
 Set-AzureADUser -ObjectId $_.UserPrincipalName  -UserPrincipalName $newupn
}

Export Users New UserPrincipalName details to CSV

Once you changed the main login name of an user using any of the above methods, you can just check it by running the below command

Get-AzureADUser -ObjectId "[email protected]" | Select DisplayName, UserPrincipalName

You can also export all azure ad users detail to csv file by running below command

Get-AzureADUser -All $True | Select DisplayName, UserPrincipalName |
Export-CSV "C:\O365Users.csv" -NoTypeInformation -Encoding UTF8
Advertisement

3 thoughts on “How to Change UPN/Sign-In Name of Office 365 user using PowerShell”

  1. Changing UPN value from: to:
    Set-AzureADUser : Cannot bind argument to parameter ‘ObjectId’ because it is null.
    At line:5 char:27
    + Set-AzureADUser -ObjectId $upn -UserPrincipalName $newupn
    + ~~~~
    + CategoryInfo : InvalidData: (:) [Set-AzureADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.SetUser

    Reply

Leave a Comment