Bulk update of User Principal Names in AD

Recently I’ve been managing Active Directory on some dev and test environments, and scripted the creation of a large number of users. Unfortunately when creating some users I’d not changed the UPNs as required when moving across environments, and AD happily allowed me to create these users with invalid UPNs. The users had been issued their passwords, so I didn’t want to have to recreate the entire set of users again, so  I decided to take to PowerShell and get the UPNs updated.

So here’s a handy little script for you if you ever need to perform a bulk update of UPNs on your Active Directory Users.


Import-Module ActiveDirectory
 $originalUPN = "@mycloud.co.uk"
 $newUPN = "@devtest.local"
 $searchOU = "OU=Standard Users,DC=devtest,DC=local"
 $filterUPN ="*"+$originalUPN
 Get-ADUser -Filter {UserPrincipalName -like $filterUPN} -SearchBase $searchOU |
 ForEach-Object {
   $oldUPN = $_.UserPrincipalName
   $updatedUPN = $oldUPN.Replace($originalUPN, $newUPN)
   Set-ADUser $_ -UserPrincipalName $updatedUPN
   write-host "Have updated user from " $oldupn " to $updatedUPN

Leave a Reply