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

PowerShell -3

In this article let us learn few basic cmd-lets. As mentioned in the first series of the article, PowerShell is very user friendly tool.You need not remember the whole command for any instance, PowerShell has the feature of auto-completion. Just type few letters in the command and press tab . PowerShell returns all the commands starting with that word and you can choose the command you want to execute.

If you are not sure about the command, its syntax and usage, you can always use PowerShell help instantly.

get-help <commandname> — briefs you about the command

get-help <commandname> -detailed  — provides detailed information and usage of the command

get-help <commandname> -examples — just provides the usage with examples

get-help <*database*> — use wild card to search for the commands with a string match

PowerShell also contains list of aliases for few cmdlets/functions. Few examples are given below.

fl – Format-List

ft – Format-tab

– For each

clc – clear content

cls – clear screen

clear – clear host

To view the list of all aliases, functions and cmdlets available in PowerShell, just open powershell and type get-command |more which returns you with all details.

Few simple commands

Show-Eventlog – Opens up event viewer for local computer

Clear – Eventlog – Clears an event log specified

Stop-Computer – Shutdown the computer specified

Test-Connection – Tests connection to a remote computer (alias for ping)

You can also convert the files to a desired format using below conversion commands ConvertFrom-CSV, ConvertTo-CSV, ConvertTo-HTML, ConvertTo-XML etc. Find out more commands using get-help *convert*

In the next articles let us learn how to import different modules, install /add different roles and features to a server.

Link to first article about PowerShell

Link to 2nd article about PowerShell