Office 365 PowerShell Tip: Bulk change UPN's

Office 365 PowerShell Tip: Bulk change UPN's

Sometimes you may provision accounts in Office 365 before any sort of domain validation.  In this case, you don't want to hit up each user account and manually change the UPN one at a time, possibly leading to setting something incorrectly.  We're going to give you some PowerShell code you can cut and paste to make this change.  This code will effectively:

  • Change the domain portion of the UPN to the domain of your choice
  • Not change the UPN of the admin account (we determine this by ignoring any UPN that begins with "admin@").  Normally you aren't going to change the admin account UPN to your own domain and will leave it as the address.


Here's a quick two line PowerShell command you can do to change all of the UPN's in your account:

  • Install the Office 365 PowerShell modules
  • Launch the command shell from the start menu (Start → All Programs → Microsoft Online Services → Microsoft Online Services Module for Windows PowerShell)
  • Copy and paste the following into the window (replace with your own domain)


Get-MsolUser -all | Where { -Not $_.UserPrincipalName.ToLower().StartsWith("admin@") } | ForEach { Set-MsolUserPrincipalName -ObjectId $_.ObjectId -NewUserPrincipalName ($_.UserPrincipalName.Split("@")[0] + "") }

As you can see, PowerShell is a very powerful tool to perform bulk operations.

Have an upcoming migration project to Microsoft365? Leverage PowerShell to automate any task in your next migration project with MigrationWiz. Check out this on-demand webinar to learn more. 

At BitTitan we've been empowering IT service professionals to successfully deploy and manage cloud technologies through automation for over a decade. Sign up for a FREE BitTitan account today to access our products and services.

Share this Post: