Skip to content

How to Add email addresses with PowerShell

You can add an additional email to a single mailbox or all mailboxes in your organization and keep the existing email addresses. It’s also possible to add an email address that will replace an existing email address. In this article, you will learn how to add email addresses with PowerShell.

Manage email addresses in Exchange Online

To manage email addresses in your Microsoft 365 organization, use the following articles:

Primary and Secondary email addresses

Each recipient can have multiple email addresses but should always have one primary SMTP address, which is the default. All the other email addresses are optional, such as:

  • smtp: Secondary email address (alias)
  • Proxy: Additional email address
  • SIP: Additional email address
  • x500: Additional email address

Connect to Exchange Online PowerShell

To run the PowerShell commands specified in the current article, you must Connect to Exchange Online PowerShell.

Connect-ExchangeOnline

Add Primary SMTP email address to single mailbox

To add or replace an existing email address, we will use the Set-Mailbox PowerShell cmdlet. We will show you different methods to add a primary email address to a single mailbox with PowerShell. You can also Change Microsoft 365 primary SMTP address in Microsoft 365 admin center and PowerShell.

Method 1. Add Primary SMTP address and remove other email addresses

We will use the -EmailAddresses parameter in the PowerShell command to replace the primary SMTP email address.

Run the below PowerShell command to add a primary SMTP email address to a user mailbox and remove the other existing email addresses.

Set-Mailbox "Brenda.Smith@m365info.com" -EmailAddresses "SMTP:BrendaNewEmail@m365info.com"

The results are as follows:

  • The new email address (BrendaNewEmail@m365info.com) replaces the current primary email address.
  • The old SMTP primary email address is now saved as a secondary smtp email address.
  • All the other additional email addresses, such as aliases, proxy, .onmicrosoft.com, and SIP addresses, have been removed.
  • The UserPrincipalName will remain the same (Brenda.Smith@m365info.com).

After you run the PowerShell command, you will get the below warning that you can ignore.

WARNING: Proxy address “Brenda.Smith@m365info.com” is used as WindowsLiveId. So it can’t be removed from list of email addresses. To remove it, first change the WindowsLiveId.

Run the below PowerShell command to check the results.

Get-Mailbox "Brenda.Smith@m365info.com" | Format-List Alias, PrimarySmtpAddress, EmailAddresses

The Powershell result shows these results.

Alias              : Brenda.Smith
PrimarySmtpAddress : BrendaNewEmail@m365info.com
EmailAddresses     : {smtp:Brenda.Smith@m365info.com,
                     SMTP:BrendaNewEmail@m365info.com}

Method 2. Add Primary SMTP address and keep all existing email addresses

We will add a primary email address that will replace the old one. To replace the primary SMTP address, use the -WindowsEmailAddress parameter.

Run the below PowerShell command to replace the primary SMTP address without removing all the other email addresses.

Set-Mailbox "Brenda.Smith@m365info.com" -WindowsEmailAddress "BrendaNewEmail2@m365info.com"

The results are as follows:

  • The new email address (BrendaNewEmail2@m365info.com) replaces the current primary email address.
  • The old SMTP primary email address is now saved as a secondary smtp email address.
  • All the other additional email addresses, such as aliases, proxy, .onmicrosoft.com, and SIP addresses remain intact.

Add Secondary smtp email address to single mailbox

There are different methods to add secondary email addresses. Use @{add in the PowerShell command to add additional email addresses.

The primary SMTP and all the other existing email addresses will remain intact for the mailbox.

Method 1

Run the below PowerShell command to add one smtp email address to a single mailbox.

Set-Mailbox "Brenda.Smith@m365info.com" -EmailAddresses @{add = "BrendaNewEmail1@m365info.com" }

Method 2

Run the PowerShell command below to add multiple smtp email addresses to a single mailbox.

Set-Mailbox "Brenda.Smith@m365info.com" -EmailAddresses @{add = "BrendaNewEmail2@m365info.com", "BrendaNewEmail3@m365info.com" }

Method 3

You can replace the add with the (+) symbol but enclose it in double quotes shown in the below PowerShell command.

Set-Mailbox "Brenda.Smith@m365info.com" -EmailAddresses @{"+" = "BrendaNewEmail4@m365info.com" }

Add Primary and Secondary email addresses to single mailbox

You can add a new primary SMTP address and secondary smtp addresses for users, but it will remove all the existing email addresses.

Method 1. Add Secondary email address and remove existing email addresses

When you add a new SIP address, it will remove all the existing email addresses for the single mailbox but keep the primary SMTP address.

Run the PowerShell command to add an SIP email address to a single mailbox.

Set-Mailbox "Brenda.Smith@m365info.com" -EmailAddresses "SIP:Brenda.Smith2@m365info.com"

The results are as follows:

  • It adds the new SIP email address (BrendaSmith2@m365info.com).
  • All the other additional email addresses, such as aliases, proxy, .onmicrosoft.com, and SIP addresses, have been removed.
  • The primary SMTP email address will remain the same.

After you run the PowerShell command, you will get the below warning that you can ignore.

WARNING: Proxy address “Brenda.Smith@m365info.com” is used as WindowsLiveId. So it can’t be removed from list of email addresses. To remove it, first change the WindowsLiveId.

Method 2. Add Primary SMTP address and Secondary email addresses and remove existing email addresses

You can replace the primary email address and add new alias addresses, which will delete all the existing email addresses. When you change the primary address, the old SMTP address will not be deleted but kept as a secondary smtp address.

In our example, we want to add these new email addresses:

  • BrendaNewEmail@m365info.com (new primary email address)
  • BrendaNew2@m365info.com
  • BrendaNew3@m365info.com

The PowerShell command Set-Mailbox will add the first email address as the Primary email address and the other email addresses as a Secondary (alias) email address if you don’t name any of the addresses.

Note: To add and replace the primary SMTP address, it’s better to add the SMTP suffix in the PowerShell command.

Run the below PowerShell command to add two email addresses.

Set-Mailbox "Brenda.Smith@m365info.com" -EmailAddresses "SMTP:BrendaNewEmail@m365info.com", "BrendaNew2@m365info.com", "BrendaNew3@m365info.com"

The results are as follows:

  • The first email address (BrendaNewEmail@m365info.com) replaces the current Primary email address.
  • It will add the second and third email addresses (BrendaNew2@m365info.com, BrendaNew3@m365info.com) as secondary (smtp) addresses.
  • The old primary address changes into a secondary smtp address.
  • All the other additional email addresses, such as aliases, proxy, .onmicrosoft.com, and SIP addresses, have been removed (deleted).
  • After running the command, you will get the warning below.

WARNING: Proxy address “Brenda.Smith@m365info.com” is used as WindowsLiveId. So it can’t be removed from list of email addresses. To remove it, first change the WindowsLiveId.

Add Secondary smtp address with new Domain name suffix for all mailboxes

We will use the ForEach statement in PowerShell to add an additional email address for all mailboxes. The PowerShell script will collect all the existing email addresses and add the new email address with the new domain name suffix as an additional email address. It will not remove the existing email addresses.

  • Our company uses the public domain name m365info.com
  • Our company purchased an additional domain name m365pilot.com

The PowerShell script will do the following:

  • Add secondary smtp address with the new domain name suffix m365pilot.com to all Exchange Online mailboxes.
  • The new email address will not replace the existing primary email address but will be added as an additional email address.
  • It will keep all the other existing email addresses, such as proxy, SIP, or x500 email addresses of the recipients.

In our example, the email addresses use the domain (m365info.com), and we will change it with another new domain (m365pilot.com) for all the mailboxes.

  1. Type the new domain name in line 3
  2. Run the below PowerShell script
$Mailboxes = Get-Mailbox -Filter { IsDirSynced -eq $false } -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes) {
    $NewAddress = $Mailbox.Alias + "@m365pilot.com"
    if ($Mailbox.EmailAddresses -notcontains "smtp:$NewAddress") {
        $Mailbox.EmailAddresses += $NewAddress
        Set-Mailbox -Identity $Mailbox.Alias -EmailAddresses $Mailbox.EmailAddresses
        Write-Host "Added secondary smtp address $NewAddress to $($Mailbox.UserPrincipalName)" -ForegroundColor Green
    }
    else {
        Write-Host "Secondary smtp address $NewAddress already exists for $($Mailbox.UserPrincipalName)" -ForegroundColor Yellow
    }
}

The PowerShell output results.

Added secondary smtp address Amanda.Hansen@m365pilot.com to Amanda.Hansen@m365info.com
Added secondary smtp address Brenda.Smith@m365pilot.com to Brenda.Smith@m365info.com
Added secondary smtp address Carl.Hawk@m365pilot.com to Carl.Hawk@m365info.com
Secondary smtp address Diana.Baker@m365pilot.com already exists for Diana.Baker@m365info.com
Secondary smtp address Frank.Olsen@m365pilot.com already exists for Frank.Olsen@m365info.com
Added secondary smtp address Julia.Wood@m365pilot.com to Julia.Wood@m365info.com
Secondary smtp address Stephen.Hunter@m365pilot.com already exists for Stephen.Hunter@m365info.com
Secondary smtp address Test.Sharedmailbox@m365pilot.com already exists for Test.Sharedmailbox@m365info.com

Add Primary SMTP address with new Domain name suffix for all mailboxes

We will replace the existing primary address with a new primary SMTP address that uses a different domain name suffix without deleting other email addresses.

  • Our company uses the public domain name m365info.com
  • Our company purchased an additional domain name m365pilot.com

The PowerShell script will do the following:

  • Replace the current primary email address with a new primary email address that uses the domain name suffix m365pilot.com.
  • It will not delete the previous primary address but change it to an alias (smtp) email address.
  • All the other existing email addresses, such as Proxy, SIP, or x500 email addresses of the recipients, will remain.

In our example, the primary email address has a domain (m365info.com) and we will change it with another new domain (m365pilot.com) for all the mailboxes. It will add the new primary email address and replace the old one.

  1. Type the new domain name in line 3
  2. Run the below PowerShell script
$Mailboxes = Get-Mailbox -Filter { IsDirSynced -eq $false } -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes) {
    $NewAddress = $Mailbox.Alias + "@m365pilot.com"
    if ($Mailbox.PrimarySmtpAddress -notlike "$NewAddress") {
        Set-Mailbox -Identity $Mailbox.Alias -WindowsEmailAddress $NewAddress
        Write-Host "Changed primary SMTP address $($Mailbox.PrimarySmtpAddress) to $NewAddress" -ForegroundColor Green
    }
    else {
        Write-Host "Primary SMTP address $NewAddress is already set for $($Mailbox.PrimarySmtpAddress)" -ForegroundColor Yellow
    }
}

The PowerShell output shows the below result.

Changed primary SMTP address Amanda.Hansen@m365info.com to Amanda.Hansen@m365pilot.com
Primary SMTP address Brenda.Smith@m365pilot.com is already set for Brenda.Smith@m365pilot.com
Changed primary SMTP address Carl.Hawk@m365info.com to Carl.Hawk@m365pilot.com
Changed primary SMTP address Diana.Baker@m365info.com to Diana.Baker@m365pilot.com
Changed primary SMTP address Frank.Olsen@m365info.com to Frank.Olsen@m365pilot.com
Changed primary SMTP address Julia.Wood@m365info.com to Julia.Wood@m365pilot.com
Primary SMTP address Stephen.Hunter@m365pilot.com is already set for Stephen.Hunter@m365pilot.com
Primary SMTP address Test.Sharedmailbox@m365pilot.com is already set for Test.Sharedmailbox@m365pilot.com

Add Primary and Secondary email addresses from CSV file

In this example, we want to replace the primary SMTP address and add secondary addresses from a CSV file without deleting any existing aliases for the recipient.

Follow these steps to create a CSV file to replace the primary SMTP address and secondary email addresses:

  1. Name the first column Recipient and list all the recipients under it
  2. Name the second column PrimaryEmail, and list all the primary email addresses
  3. Name the third column AliasEmail, and list all the email addresses
  4. Use the semicolon (;) character to separate between email addresses under the AliasEmail column
Add email addresses with PowerShell from CSV file primary and secondary
  1. Name the file EmailAddresses and save it as a CSV file
  2. Create a temp folder in the (C:) drive if you don’t have one
  3. Save the EmailAddresses.csv file in the temp folder
  4. To ensure PowerShell can read the file, run the Import-Csv cmdlet
Import-Csv "C:\temp\EmailAddresses.csv"
  1. Specify the CSV path file in line 1
  2. Run the below PowerShell script
$Recipients = Import-Csv "C:\temp\EmailAddresses.csv"
foreach ($Mailbox in $Recipients) {
    $PrimaryEmail = $Mailbox.PrimaryEmail
    $AliasEmails = $Mailbox.AliasEmail -split ';'

    # Get current email addresses of the mailbox
    $EXOMailbox = Get-Mailbox -Identity $Mailbox.Recipient

    foreach ($Alias in $AliasEmails) {
        # Check if alias already exists
        if ($EXOMailbox.EmailAddresses -notcontains "smtp:$Alias") {
            Set-Mailbox -Identity $Mailbox.Recipient -EmailAddresses @{add = "smtp:$Alias" } -WarningAction SilentlyContinue
            Write-Host "Adding alias email address '$Alias' to $($Mailbox.Recipient)" -ForegroundColor Green
        }
        else {
            Write-Host "Alias email address '$Alias' already exists for $($Mailbox.Recipient). Skipping..." -ForegroundColor Yellow
        }
    }
    # Set the primary email address if it's different from the current primary email
    if ($PrimaryEmail -ne $EXOMailbox.PrimarySMTPAddress) {
        Set-Mailbox -Identity $Mailbox.Recipient -WindowsEmailAddress $PrimaryEmail -WarningAction SilentlyContinue
        Write-Host "Setting primary email address to '$PrimaryEmail' for $($Mailbox.Recipient)" -ForegroundColor Green
    }
    else {
        Write-Host "Primary email address '$PrimaryEmail' is already set for $($Mailbox.Recipient). Skipping..." -ForegroundColor Yellow
    }
}

The PowerShell script will do the following:

  • Add the new alias smtp addresses
  • Set a new primary SMTP address
  • The old primary SMTP address will change into an alias smtp address
  • It will also keep all the existing email addresses of the recipient
  • It shows if the primary and alias email addresses are already set
Adding alias email address 'Brenda.A1@m365info.com' to Brenda.Smith@m365info.com
Adding alias email address 'Brenda.A2@m365info.com' to Brenda.Smith@m365info.com
Alias email address 'Brenda.A3@m365info.com' already exists for Brenda.Smith@m365info.com. Skipping...
Setting primary email address to 'Brenda.Smith1@m365info.com' for Brenda.Smith@m365info.com

Add Secondary smtp email addresses from CSV file

In the next example, we only want to add secondary smtp addresses from a CSV file without deleting any existing aliases for the recipient.

You can choose to add a single or multiple secondary smtp email addresses to each recipient in the CSV file.

You need to create a CSV file:

  1. Name the first column Recipient and list all the recipients under it
  2. Name the third column, AliasEmail, and list all the email addresses under it
  3. Use the semicolon (;) character to separate between email addresses under the AliasEmail column.
Add email addresses with PowerShell from CSV file secondary
  1. Name the file SecondaryAddresses and save it as a CSV file
  2. Create a temp folder in the (C:) drive if you don’t have one
  3. Save the SecondaryAddresses.csv file in the temp folder
  4. To ensure PowerShell can read the file, run the Import-Csv cmdlet
Import-Csv "C:\temp\SecondaryAddresses.csv"
  1. Specify the CSV path file in line 1
  2. Run the below PowerShell script
$Recipients = Import-Csv "C:\temp\SecondaryAddresses.csv"
foreach ($Mailbox in $Recipients) {
    $AliasEmails = $Mailbox.AliasEmail -split ';'

    # Get current email addresses of the mailbox
    $EXOMailbox = Get-Mailbox -Identity $Mailbox.Recipient

    foreach ($Alias in $AliasEmails) {
        # Check if alias already exists
        if ($EXOMailbox.EmailAddresses -notcontains "smtp:$Alias") {
            Set-Mailbox -Identity $Mailbox.Recipient -EmailAddresses @{add = "smtp:$Alias" } -WarningAction SilentlyContinue
            Write-Host "Adding alias email address '$Alias' to $($Mailbox.Recipient)" -ForegroundColor Green
        }
        else {
            Write-Host "Alias email address '$Alias' already exists for $($Mailbox.Recipient). Skipping..." -ForegroundColor Yellow
        }
    }
}

That’s it!

Read more: How to Assign Microsoft 365 licenses with PowerShell »

Conclusion

You learned how to add email addresses with PowerShell. If you only want to add email addresses for a single mailbox, you can use the Exchange admin center or PowerShell. However, changing the primary SMTP address and secondary email addresses for multiple mailboxes is faster with PowerShell.

Did you enjoy this article? You may also like Manage Microsoft 365 users password. Don’t forget to follow us and share this article.

o365info Team

o365info Team

This article was written by our team of experienced IT architects, consultants, and engineers.

This Post Has 0 Comments

Leave a Reply

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