Change UPN (based on Primary Email) based on SMTP proxy address

During an Exchange migration, where the users have several domains spread among them, I needed to find a script that looks into the SMTP Proxyaddress of the user object, and changes the UPN for that user to match the ProxyAddress.

I did’nt have to search long, before I found this page:

Change UPN (based on Primary Email) based on SMTP: in proxy addresses

Copy the following into a .ps1 file and run it directly from Powershell (make sure you have AD PowerShell CMD’lets available).. it will by default NOT make any changes, but it will create an output file you can validate. Once you are ready to execute run the script with /tm:no as an option.

Param($Param2)
$bolTestMigration=$true
If ($Param2 -eq "/tm:no") {
Write-Host "Production Migration - Making Changes!"
$bolTestMigration=$false}
ELSE {$bolTestMigration=$true}
 $ScriptLocation="C:Scripts"
$DateStamp = get-date -uformat "%Y-%m-%d-%H-%M-%S"
$Logfile = $Logfile = ($ScriptLocation + "UPNSET-" + $DateStamp + ".log")
 Function LogWrite
{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
Write-Host $logstring
}
 Write-Host " Loading Active Directory cmdlets"
Import-Module ActiveDirectory
 #For each object in our environment, we are going to look up the proxyAddresses, get the address that starts with SMTP: and use that as the UPN
$CollObjects=Get-ADObject -LDAPFilter "(&(legacyExchangeDN=*)(objectClass=user))" -Properties ProxyAddresses,distinguishedName,userPrincipalName
Write-Host $CollObjects.count
foreach ($object in $CollObjects){
$Addresses = ""
$DN=""
$UserPrincipalName=""
#Write-Host "Found: " $object.DisplayName
$Addresses = $object.proxyAddresses
$ProxyArray=""
$DN=$object.distinguishedName
ForEach ($Address In $Addresses)
{
$ProxyArray=($ProxyArray + "," + $Address)
If ($Address -cmatch "SMTP:")
{
$PrimarySMTP = $Address
$UserPrincipalName=$Address -replace ("SMTP:","")
#Found the object validating UserPrincipalName
If ($object.userPrincipalName -notmatch $UserPrincipalName) {
If ($bolTestMigration -eq $false) {
Write-Host "." -ForegroundColor Blue
LogWrite ($DN + ";" + $object.userPrincipalName + ";NEW:" + $UserPrincipalName)
Set-ADObject -Identity $DN -Replace @{userPrincipalName = $UserPrincipalName}
}
If ($bolTestMigration -eq $true) {
Write-Host "." -ForegroundColor Blue
LogWrite ($DN + ";" + $object.userPrincipalName + ";NEW:" + $UserPrincipalName)
Write-Host "was:" $object.userPrincipalName
Write-Host "setting:" $UserPrincipalName
Set-ADObject -Identity $DN -WhatIf -Replace @{userPrincipalName = $UserPrincipalName}
}
 }
ELSE {
Write-Host "." -ForegroundColor Green -NoNewline
}
}
}
}

Tested this on Windows Server 2012 R2 DC and with Exchange 2013, all worked perfect! (Remember to add the UPNs in AD before you go 😉 )

Credits to Roelf Zomerman for this 😉

Leave a Comment

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

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

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close