A user can have multiple email addresses in Exchange. However, only one of them is his primary e-mail address. So what happens if you want to change one of the other addresses to your primary e-mail address?
Index
Multiple email addresses in Exchange
All email addresses of a user are listed in Exchange. The default response address is displayed in bold on the following picture:
Synchronisation of email addresses in AD
These addresses are also synchronised from Exchange to your Active Directory. The value in the attribute “mail” is the primary address. It is also convenient that a similar view with all the addresses as in Exchange is available via the attribute “proxyAdresses”:
Change the primary email address in Exchange
The Exchange admin interface makes it easy to change the primary email address. With Powershell it is a little more complicated, but also possible.
It is important to know how Exchange internally determines which email address is the primary one. Unfortunately, this is not as clear regulated as in the ActiveDirectory. Internally, Exchange stores all email addresses in the same attribute, together with a protocol prefix, that is also visible in the management interface in Exchange. Usually this is “smtp:”. The primary email address has a small but important difference: The “SMTP:” in front of the address is in capital letters.
So if we want to set one of the proxy addresses as primary, we need to capitalize the SMTP prefix of this one and lowercase the prefixes of all other addresses.
Remote access to Exchange with PowerShell
In this example, we want to access the Exchange remotely, i.e. we do not want to work directly with the Exchange Management Console. For this, we need a PS session. We import the Exchange session of the server via the URL, which is composed as follows:
http: // <Name or IP of the server> / PowerShell
Import Get-Mailbox and Set-Mailbox
In addition, we will not import the entire session but only the CmdLets “Get-Mailbox” and “Set-Mailbox” , since we do not need others. The session import looks like this:
1 2 3 4 |
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “http://exc16/PowerShell” -Authentication Kerberos Import-PSSession $session -CommandName Get-Mailbox, Set-Mailbox |
First we need an identifier for the user (his login name, primary email address or objectGUID) whose mailbox we want to edit. So we search out his mailbox:
1 |
$mb = Get-Mailbox steve.koenig |
Additionally we initialise an empty arraylist where we will store our email addresses.
1 |
$list = New-Object System.Collections.ArrayList |
Set a NewPrimaryMail
Then, we determine which mail address should be the new primary address. This should already be assigned to the mailbox.
1 |
$newPrimaryMail = "koenig.steve@demofa.net" |
In the next step we iterate over the email addresses of the mail. Since these are entered with the prefix, we simply split them up at the colon and remember both the prefix and the address itself.
1 2 3 4 |
foreach($address in $mb.EmailAddresses) { $prefix = $address.Split(":")[0] $mail = $address.Split(":")[1] } |
Now comes the step to the actual change of the primary mail address: We compare the address with our $ newPrimaryMail. If they are the same, we add them to our list with the capitalized prefix “SMTP:”. If not, then it is another address we add to the list with the already entered prefix, but in lower case.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
foreach($address in $mb.EmailAddresses) { $prefix = $address.Split(":")[0] $mail = $address.Split(":")[1] if ($mail.ToLower() -eq $newPrimaryMail.ToLower()) { $address = "SMTP:" + $mail } else { $address = $prefix.ToLower() + ":" + $mail } $list.Add($address) } |
So our list should now contain our new primary address with a capital “SMTP” as prefix and everything else with a lowercase prefix.
Transfer list to mailbox
Finally we just have to pass the list to the mailbox and overwrite the old one with it (and do not forget to close the PS session again):
1 2 |
Set-Mailbox steve.koenig -EmailAddresses $list Remove-PSSession $session |
The changes are immediately visible in the Exchange Management and will synchronise from Exchange to AD afterwards.
The Complete Script: Changing the Primary Email Address with PowerShell
Finally, here is the entire script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “http://exc16/PowerShell” -Authentication Kerberos Import-PSSession $session -CommandName Get-Mailbox, Set-Mailbox $mb = Get-Mailbox steve.koenig $newPrimaryMail = "koenig.steve@demofa.net" $list = New-Object System.Collections.ArrayList foreach($address in $mb.EmailAddresses) { $prefix = $address.Split(":")[0] $mail = $address.Split(":")[1] if ($mail.ToLower() -eq $newPrimaryMail.ToLower()) { $address = "SMTP:" + $mail } else { $address = $prefix.ToLower() + ":" + $mail } $list.Add($address) } Set-Mailbox steve.koenig -EmailAddresses $list Remove-PSSession $session |
FirstAttribute AG – Identity Management & IAM Cloud Services
We would be happy to present our services and solutions to you. Get in touch and find out how we can help you.
Leave a Reply
<p>Your email is safe with us.<br/>Information about our <a href="https://activedirectoryfaq.com/contact-us/">data protection policies</a></p>