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.

Advertisements

How to save sent items in different folder incase of shared/resource mailboxes

Whenever user often accesses a shared mailbox, we add the shared  mailbox as an additional mailbox in each user’s Outlook profile.
This works fine, except for the fact that e-mail messages sent from the additional mailbox go into the “Sent items” folder of the primary mailbox within a user’s profile. In order for the other users accessing the shared mailbox to keep track of the activity in the shared mailbox, it’s important for us that outgoing messages are stored in the “Sent items” folder of the shared mailbox.
This is a common situation, and not just with Exchange 2010 and Outlook 2010. Although it isn’t well known, we can change this standard behavior using an Outlook client-specific registry key. When adding the registry key, when you reply to or forward e-mail messages located in the shared mailbox, they actually go into the expected “Sent items” folder.
To change the “Sent items” behavior, you need to create the following registry key
Key:
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\Preferences
Name:
DelegateSentItemsStyle
Type:
DWORD
Value:
1
Note that it needs to be created on HKEY_CURRENT_USER.
 Changing where items sent from a shared mailbox are stored
requires a new registry key.