Swap Primary and Secondary email addresses using PowerShell

One or the other time, every system administrator would need to swap primary & secondary mail addresses due to various reasons like company merge, acquire, branding etc.

In such cases, there would be a need to swap primary and secondary email addresses in a phase-wised manner. In such scenarios, below scripts might help you.

  • Change the addresses for list of users from a CSV file

*****************************************************************************************

## to save results in output file, you can specify your own path.

$Output = “c:\scripts\mailaddresschange_results.txt”
Out-File $Output -InputObject “OldPrimarySMTPAddress`tNewPrimarySMTPAddress”

##Specify your new primary SMTP domain name without @

$NewDomain = “newSMTPdomain”

##Import the CSV file here; you can specify your own path. You can specify your own path, make sure that the list has some header specified such as displayName, givenName, alias (specify the attribute name as appears in AD

$list = import-csv “c:\scripts\userslist.csv”

foreach ($_.displayname in $list)

{ $mb = Get-mailbox $_.displayName
#capture current primary smtp address
$SMTP = $mb.PrimarySmtpAddress
[string]$Local = $SMTP.Local
[string]$OldDomain = $SMTP.Domain
[string]$CPSMTP = $Local + “@” + $OldDomain
#capture new primary smtp address
[string]$NPSMTP = $Local + “@” + $NewDomain
#capture the old and the new SMTP addresses to the output file
[string]$iobject = $CPSMTP + “`t” + $NPSMTP
Out-File $Output -InputObject $iobject –Append
#set the new primary smtp address on the mailbox and remove the flag to use the email address policy (if you do not do this, the email address will revert to whatever the policy has set to)
Set-Mailbox $_.displayName -PrimarySmtpAddress $NPSMTP -EmailAddressPolicyEnabled $false
}

*****************************************************************************************

  • Change the addresses for users in particular OU

### to save results in output file, you can specify your own path.

$Output = “c:\scripts\mailaddresschange_results.txt”
Out-File $Output -InputObject “OldPrimarySMTPAddress`tNewPrimarySMTPAddress”

#specify the OU you wish to pull the users from

$OU = “domainFQDN/OU1/OU2”

# Specify your new primary SMTP domain name without @

$NewDomain = “newSMTPdomain.com”

#get list of all mailboxes in the OU

$list = get-mailbox -OrganizationalUnit $OU -resultsize Unlimited

#Iteration for the list

foreach ($user in $list)

{

$mb = Get-mailbox $user

#capture current primary smtp address

$SMTP = $mb.PrimarySmtpAddress

[string]$Local = $SMTP.Local

[string]$OldDomain = $SMTP.Domain

[string]$CPSMTP = $Local + “@” + $OldDomain

#captur new primary smtp address

[string]$NPSMTP = $Local + “@” + $NewDomain

#capture the old and the new SMTP addresses to the output file

[string]$iobject = $CPSMTP + “`t” + $NPSMTP

Out-File $Output -InputObject $iobject -Append

#set the new primary smtp address on the mailbox and remove the flag to use the email address policy (if you do not do this, the email address will revert to whatever the policy has set to)

Set-Mailbox $user -PrimarySmtpAddress $NPSMTP -EmailAddressPolicyEnabled $false

}

*************************************************************************************************************

If the user group is small or if you want to do it for all users in a domain, instead of using above scripts you can just change the settings in your email address policy either through Exchange Management Console (EMC) or Exchange Management Shell (EMS) as mentioned below.

  • EMC: Open EMC –> Expand Organization Configuration –> Hub Transport –> Email Address Policies
  • EMS:  Use Set-EmailAddressPolicy cmdlet with required parameters

Hope this would be useful to all of you.

Advertisement