Sometimes you may need to change a user’s primary UPN suffix in Active Directory, commonly if you’re setting up Office 365 and you have a different external to internal domain name. This can be done manually per user in Active Directory or even per entire OU but if you have lots of users in different OUs the manual process can become quite laborious. To script this using Powershell the following code will search through each User in the OU specified in the Search Base and replace their UPN suffix from the old suffix to the new suffix.
Import Module ActiveDirectory Get-ADUser -Filter {UserPrincipalName -like “*@OLDSUFFIX.local”} -SearchBase “OU=Staff,DC=oldsuffix,DC=local” | ForEach-Object { $UPN = $_.UserPrincipalName.Replace(“OLDSUFFIX.local”,”newsuffix.com”) Set-ADUser $_ -UserPrincipalName $UPN } Get-ADUser -Filter {UserPrincipalName -like “*@oldsuffix.local”} -SearchBase “OU=Staff,DC=oldsuffix,DC=local” | ForEach-Object { $UPN = $_.UserPrincipalName.Replace(“oldsuffix.local”,”newsuffix.com”) Set-ADUser $_ -UserPrincipalName $UPN }
Note this is repeated twice for uppercase and lowercase as the replace function is case sensitive.

Leave a Reply

Your email address will not be published. Required fields are marked *