How to change the UPN for a list of users with powershell

There is a requirement as part of domain migrations that none of the domains being released can be referenced in a UPN. In order to tackle this requirement I will need to change the UPN suffix for all the affected users.

To get a list of all our target users we will first need to follow the steps found on this page:

How to export sip and smtp addresses from target domains

Now that i’ve got my target users, i can add them into a excel csv that contains the target users samaccount names.

The excel column containing the data will need a heading of “loginid”

I then Saved the csv to the C:

Now im able to run the script below which loops through the entries and changes the UPN suffix from the old suffix to the new one.

#This is the old upn
$oldSuffix = 'notbadtech.sg'

#This is the new upn that you want to use
$newSuffix = 'notbad.tech'

#This will import the csv and loop through each object - for each object it will get the upn and samaccountname
Import-CSV C:\Temp\ChangeUPN.csv | ForEach-Object {
$usr = get-aduser $_.loginid |Select userprincipalname, samaccountname

#This will then replace the old upn with the new upn and set the attribute on each user. one by one looping through the csv.
$newUpn = $usr.UserPrincipalName.Replace($oldSuffix,$newSuffix)
Set-ADUser -identity $usr.samaccountname -UserPrincipalName $newUpn -Verbose -Debug
} 

1 Reply to “How to change the UPN for a list of users with powershell”

Leave a Reply

Your email address will not be published.