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:

  1. Change the domain portion of the UPN to the domain of your choice
  2. 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 onmicrosoft.com address.

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

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

Connect-MsolService

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

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

 

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of