How to change the UserPrincipalName (UPN) of a user from one federated domain to another…

If you’re reading this then you’ve probably tried to change the UPN of a synced user from one federated domain to another, wondered why it’s not synced properly and then tried to do it manually and encountered this error:

Set-MsolUserPrincipalName : Unable to complete this action. Try again later.

The reason you’re getting this is because for some reason (the reason is unknown to me) you can’t change the UPN of a user directly from one federated domain to another. When I say federated domain, I mean the verified domains in your O365 portal which aren’t your initial onmicrosoft.com domain.

To get around this you first need to change the UPN to your initial onmicrosoft.com domain and then to the target verified domain. So, two commands to perform one action:

Adjust the three variables at the top of this short script and run it: