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?
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:
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:
Additionally we initialise an empty arraylist where we will store our email addresses.
Set a NewPrimaryMail
Then, we determine which mail address should be the new primary address. This should already be assigned to the mailbox.
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.
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.
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):
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:
0 Comments