PowerShell to set New UPN, Primary and Secondary Proxy Addresses on the Active Directory.

For Single User
$user = "account name"
$primarySMTP = "Name@primarysmtp.com"
$secondarySMTP = "nName@secsmtp.com"
$newUPN = "name@newupn.com" # Update this to the new UPN

# Update the UPN
Set-ADUser -Identity $user -UserPrincipalName $newUPN

# Set the primary email address
Set-ADUser -Identity $user -EmailAddress $primarySMTP

# Add the primary SMTP address to proxyAddresses
Set-ADUser -Identity $user -Add @{proxyAddresses="SMTP:$primarySMTP"}

# Add the secondary SMTP address to proxyAddresses
Set-ADUser -Identity $user -Add @{proxyAddresses="smtp:$secondarySMTP"}
For bulk Users.

CSV File Format

UsernamePrimarySMTPSecondarySMTPNewUPNSuffix
jdoejdoe@prismtp.comjdoe@secsmtp.comnewdomain.com
asmithasmith@prismtp.comasmith@secsmtp.comnewdomain.com
# Import the CSV file
$users = Import-Csv -Path "C:\path\to\users.csv"

foreach ($user in $users) {
    $username = $user.Username
    $primarySMTP = $user.PrimarySMTP
    $secondarySMTP = $user.SecondarySMTP
    $newUPNSuffix = $user.NewUPNSuffix
    
    # Retrieve the current user's UPN
    $currentUPN = (Get-ADUser -Identity $username -Properties UserPrincipalName).UserPrincipalName

    # Extract the username from the current UPN
    $usernamePart = $currentUPN.Split('@')[0]

    # Construct the new UPN
    $newUPN = "$usernamePart@$newUPNSuffix"

    # Update the UPN
    Set-ADUser -Identity $username -UserPrincipalName $newUPN

    # Set the primary email address
    Set-ADUser -Identity $username -EmailAddress $primarySMTP

    # Add the primary SMTP address to proxyAddresses
    Set-ADUser -Identity $username -Add @{proxyAddresses="SMTP:$primarySMTP"}

    # Add the secondary SMTP address to proxyAddresses
    Set-ADUser -Identity $username -Add @{proxyAddresses="smtp:$secondarySMTP"}
}

Leave a Comment