ExchangeOffice 365

Add mail.onmicrosoft.com routing addresses without an email address policy

I came across an issue for a client today where 90% of the mailboxes were set to not inherit the email address policy from exchange. Furthermore the policy was different to the actual email addresses so I could not simply turn the policy on for the accounts to inherit the required mail.onmicrosoft.com routing addresses.

The company use first.last@domain.com but the policy was set to alias@domain.com so bulk enabling the setting would have modified everyone’s reply address!

Normally I would edit the policy to match the required format (first.last) then enable it on the mailboxes, however this customer also has multiple primary email domains so this was difficult without multiple policies and scoping the policies to the correct mailboxes. The user accounts with the different primary email addresses were scattered amongst several OU’s which made scoping too challenging to use email address policies to achieve the task.

I turned to PowerShell to solve the problem

This was achieved by filtering out any accounts that already had the alias.mail.onmicrosoft.com address as we didn’t want to end up with multiple different entries. Next the script injects the routing address using the @{add} method on the set-mailbox cmdlet to add the additional address without touching the primary or affecting any other addresses.

The script syntax is as follows

$allmailboxes = get-mailbox -resultsize unlimited
foreach ($mailbox in $allmailboxes){
if ($mailbox.emailaddresses -like "*onmicrosoft.com*") {
write-host $mailbox.name "Has a 365 address...skipping...." -ForegroundColor Yellow
} else {
write-host $mailbox.name " Does not have a onmicrosoft.com address - adding..."
#ConstructAddress
$OnMicrosoftAddress = $mailbox.alias + "@YOURTENANT.mail.onmicrosoft.com"
set-mailbox $mailbox -EmailAddresses @{add=$OnMicrosoftAddress}
write-host "Added" -ForegroundColor Green
}
}

I am planning to build this out further to include more verbose reporting and a whatif mode before publishing it on the TechNet gallery.

365 Guy

Microsoft Infrastructure Consultant @ Novosco. Specialising in Office 365, Azure AD & Exchange Server.

Leave a Reply

Your email address will not be published. Required fields are marked *

Close