Change UPN of All Microsoft 365 Users
Here’s a useful script you can use when you need to change the UPN and primary email address of all your users in a Microsoft 365 Tenant.
Here’s a useful script you can use when you need to change the UPN and primary email address of all your users in a Microsoft 365 Tenant.
Prerequisites:
- You’ll need the AzureAD.Preview PowerShell module.
- You’ll need to be a Global Administrator of the Azure AD tenant.
Simply change the $NewDomain variable to your new company domain, and change the $OldDomain variable to domain of the user’s you want to change.
Run, and away you go!
$NewDomain = "newdomain.co.uk"
$OldDomain = "olddomain.co.uk"
Get-AzureADUser -All $True | Where { $_.UserPrincipalName.ToLower().EndsWith($OldDomain) } |
ForEach {
$newupn = $_.UserPrincipalName.Split("@")[0] + "@" + $NewDomain
Write-Host "Changing UPN value from: "$_.UserPrincipalName" to: " $newupn -ForegroundColor Yellow
Set-AzureADUser -ObjectId $_.UserPrincipalName -UserPrincipalName $newupn
}