Change UPN For Multiplue Active Directory Users Using PowerShell

In this blog post, I will show you how I change the UPN of multiple Active Directory users using PowerShell.

In my case, I have a list of Active Directory user that I need to change their UPN from company a to company b.

Because I would like to automate this process, I am using a CSV file and a PowerShell script to apply the changes.


In this demonstration, I am using a CSV file with a list of samaccountname of users that I need to change their UPN as shown below.



the code below, takes the old and new UPNs and store them in variables and replace the UPNs.

$oldSuffix = 'myolddomain.local'
$newSuffix = 'newdomain.local'
Import-CSV .\List.csv | ForEach-Object 
$usr = get-aduser $_.loginid |Select userprincipalname, samaccountname
$newUpn = $usr.UserPrincipalName.Replace($oldSuffix,$newSuffix)
Set-ADUser -identity $usr.samaccountname -UserPrincipalName $newUpn -Verbose 

Check after change

To check if the code had worked.

Import-Csv .\List.csv |  Foreach {Get-ADUser $_.loginid  -Properties *  | Select SAMAccountName, displayname, UserPrincipalName, EmailAddress}


Leave a Reply

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