How to Add SMTP Email Alias and Set as Primary SMTP Address in Exchange Online Using PowerShell

Follow this simple PowerShell script to quickly bulk add a new email alias and set to primary SMTP address for all users in Exchange Online.

Below is a very simple PowerShell script I wrote in order to bulk add an SMPT alias to all users in Office 365 Exchange Online and set the new email address as their primary SMTP address. The process is listed in the few steps below.

Prerequisites

  • ExchangeOnlineManagement PowerShell Module – Install-Module -Name ExchangeOnlineManagement -Scope AllUsers
  • Import-Module ExchangeOnlineManagement
  • Connect-ExchangeOnline

Explanation of the Script

  • Get-Mailbox line gets all the users in exchange online and sets those to a variable for use in the foreach loop
  • $domain line just sets up the domain suffix I plan to use for the new alias (replace ‘@mydomain.com’ with your suffix
  • the rest is noted in the code after the ‘#’ hash symbol
$users = Get-Mailbox -Resultsize Unlimited
$domain = "@mydomain.com"
foreach ($user in $users)
{
   $prim = $user.PrimarySmtpAddress; # Get the user's primary SMTP address
   $prefix = $prim.Substring(0, $prim.IndexOf("@")); # Remove the suffix from the SMTP Address
   $newSMTP = $prefix + $domain; # String the user's prefix and new suffix together
   Set-Mailbox $prefix -EmailAddresses @{add=$newSMTP} # Add the new SMTP alias
   Set-Mailbox $prefix -WindowsEmailAddress $newSMTP # Set the new SMTP as the primary email
} 

Leave a Comment