Function Fix-O365UPNMisMatch { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] $UserAzureUPN , [Parameter(Mandatory=$true)] $UserNewAzureUPN , [Parameter(Mandatory=$true)] $UserTempAzureUPN ) Connect-MsolService $user = Get-MsolUser -UserPrincipalName $UserAzureUPN -EA STOP try { Set-MsolUserPrincipalName -ObjectId $user.ObjectId -NewUserPrincipalName $UserTempAzureUPN -EA STOP try { Set-MsolUserPrincipalName -ObjectId $user.ObjectId -NewUserPrincipalName $UserNewAzureUPN -EA STOP } catch { "Could not set $($user.UserPrincipalName) to new UPN: $UserNewAzureUPN EXCEPTION: $($_.Exception.Message)" } } catch { Write-Host "Could not set $($user.UserPrincipalName) to temporary UPN: $UserTempAzureUPN EXCEPTION: $($_.Exception.Message)" } } Fix-O365UPNMisMatch -UserAzureUPN "oldupn@mycompany.com" -UserTempAzureUPN "oldupntemp@mytennant.onmicrosoft.com" -UserNewAzureUPN newupn@mycompany.co.uk"