Changing User Principal Names in Bulk on Azure Active Directory

By | August 12, 2016

Now that you have successfully synchronized your local Active Directory to your Azure Active Directory you found out that you wrongly used a different UPN now how do you quickly fix the issue and change them in bulk.  Well its quite easy specially with Powershell you can do this in bulk, all you need is to connect to your Azure using Connect-MSOLService then once connected just get all users you need by filtering the results of Get-MsolUser and from there you have your users all you need is update the UPN’s as needed by using Set-MsolUserPrincipalName command.

AD Azure

All you need now is to have an admin user that has the rights to update users on Azure Active Directory and start coding

#Connect to Azure AD
Connect-MSOLService
 
#Get all users with the Old User Principal
 
$users = Get-MsolUser | Where {$_.UserPrincipalName -like "*oldDomainName.com"} | select UserPrincipalName
 
#Loop Through All Users Found
 foreach ($user in $users) {
 
   #Create New User Principal Name
   $newUser = $user.UserPrincipalName -replace "oldDomainName.com", "newDomainName.com"
 
   #Set New User Principal Name
   Set-MsolUserPrincipalName -UserPrincipalName $user.UserPrincipalName -NewUserPrincipalName $newUser
 
   #Display New User Principal Name
   $newUser
 }

So what this does is commented on that code and it is quite straightforward to follow.

Recommended

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.