skip to Main Content

Bulk add secondary SMTP address with PowerShell

In the previous article, you learned how to find a missing SMTP address. Now that you have the information, you like to add a secondary SMTP address to the mailboxes. You can add the secondary SMTP address manually, but automating it with a PowerShell script is faster. In this article, you will learn how to bulk add secondary SMTP address with PowerShell.

Introduction

The Add-SMTP.ps1 PowerShell script works for:

  • Exchange on-premises
  • Exchange hybrid
  • Exchange Online

Prepare the add SMTP address PowerShell script

Download the Add-SMTP.ps1 script or copy and paste the below code in Notepad. Give it the name Add-SMTP.ps1 and place it in the C:\scripts folder. Create a scripts folder if you don’t have one.

# Output will be added to C:\temp folder. Open the Add-SMTP-Address.log with a text editor. For example, Notepad.
Start-Transcript -Path C:\temp\Add-SMTP-Address.log -Append

# Get all mailboxes
$Mailboxes = Get-Mailbox -ResultSize Unlimited

# Loop through each mailbox
foreach ($Mailbox in $Mailboxes) {

    # Search for specified SMTP address in every mailbox
    $SMTPAddress = $Mailbox.EmailAddresses | Where-Object { $_ -like "*@contoso.com" }
      
    # Do nothing when there is already an SMTP address configured
    If (($SMTPAddress | Measure-Object).Count -eq 0) {
	
        # Change @contoso.com to the domain that you want to add
        $Address = "$($Mailbox.Alias)@contoso.com"

        # Remove the -WhatIf parameter after you tested and are sure to add the secondary email addresses
        Set-Mailbox $Mailbox.Name -EmailAddresses @{add = $Address } -WhatIf

        # Write output
        Write-Host "Adding $($Address) to $($Mailbox.Name) Mailbox" -ForegroundColor Green
    }
}

Stop-Transcript

Change the @contoso.com value on both places to the domain that you want to add to the mailboxes.

In my example, this is how it looks. It will look for mailboxes with the domain @alitajran.com. If it’s not set, the SMTP address alias@alitajran.com is added to the mailbox.

# Output will be added to C:\temp folder. Open the Add-SMTP-Address.log with a text editor. For example, Notepad.
Start-Transcript -Path C:\temp\Add-SMTP-Address.log -Append

# Get all mailboxes
$Mailboxes = Get-Mailbox -ResultSize Unlimited

# Loop through each mailbox
foreach ($Mailbox in $Mailboxes) {

    # Search for specified SMTP address in every mailbox
    $SMTPAddress = $Mailbox.EmailAddresses | Where-Object { $_ -like "*@alitajran.com"}
      
    # Do nothing when there is already an SMTP address configured
    If (($SMTPAddress | Measure-Object).Count -eq 0) {
	
        # Change @contoso.com to the domain that you want to add
        $Address = "$($Mailbox.Alias)@alitajran.com"

        # Remove the -WhatIf parameter after you tested and are sure to add the secondary email addresses
        Set-Mailbox $Mailbox.Name -EmailAddresses @{add = $Address } -WhatIf

        # Write output
        Write-Host "Adding $($Address) to $($Mailbox.Name) Mailbox" -ForegroundColor Green
    }
}

Stop-Transcript

In the next step, you are going to run the script and see it in action.

Bulk add secondary SMTP address PowerShell script

The script works for Exchange on-premises, Exchange hybrid, and Exchange Online. You must connect with the proper tools before you run the script:

Go to the script path and run the Add-SMTP.ps1 script. The script will go through all the mailboxes in the Exchange Organization. The -WhatIf parameter is added in the script. If you run the script, nothing will happen in the environment. You will get an output showing what will happen.

In my example, the SMTP address @alitajran.com will be added in bulk. After running the script, confirm that these are the SMTP addresses that need to be added.

[PS] C:\> cd \scripts
[PS] C:\scripts>.\Add-SMTP.ps1
Transcript started, output file is C:\temp\Add-SMTP-Address.log
What if: Setting mailbox "exoip.local/Users/Administrator".
Adding Administrator@alitajran.com to Administrator Mailbox
What if: Setting mailbox "exoip.local/Company/Users/HR/Amanda Morgan".
Adding amanda.morgan@alitajran.com to Amanda Morgan Mailbox
What if: Setting mailbox "exoip.local/Company/Users/HR/Christopher Payne".
Adding christopher.payne@alitajran.com to Christopher Payne Mailbox
What if: Setting mailbox "exoip.local/Company/Users/ServiceAccounts/Mary Walsh".
Adding mary.walsch@alitajran.com to Mary Walsh Mailbox
What if: Setting mailbox "exoip.local/Company/Users/HR/Dylan Piper".
Adding Dylan.Piper@alitajran.com to Dylan Piper Mailbox
What if: Setting mailbox "exoip.local/Company/Users/ServiceAccounts/Benetiz Anees".
Adding Benetiz.Anees@alitajran.com to Benetiz Anees Mailbox
Transcript stopped, output file is C:\temp\Add-SMTP-Address.log

Remove the -WhatIf parameter from the PowerShell script and rerun the script. The SMTP addresses are added in bulk.

[PS] C:\scripts>.\Add-SMTP.ps1
Transcript started, output file is C:\temp\Add-SMTP-Address.log
Adding Administrator@alitajran.com to Administrator Mailbox
Adding amanda.morgan@alitajran.com to Amanda Morgan Mailbox
Adding christopher.payne@alitajran.com to Christopher Payne Mailbox
Adding mary.walsch@alitajran.com to Mary Walsh Mailbox
Adding Dylan.Piper@alitajran.com to Dylan Piper Mailbox
Adding Benetiz.Anees@alitajran.com to Benetiz Anees Mailbox
Transcript stopped, output file is C:\temp\Add-SMTP-Address.log

I hope that this helped you to bulk add SMTP addresses to the mailboxes in the Exchange organization.

Read more: List users not inheriting email address policy »

Conclusion

In this article, you learned how to bulk add secondary SMTP address with PowerShell. Download the Add-SMTP PowerShell script. Add the domain that you like to add and run the script. The script output will show which proxy addresses are added. Don’t forget to test first with the -WhatIf parameter, as shown in the article.

Did you enjoy this article? You may also like Get Mailbox size of all users in Exchange with PowerShell. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 8 Comments

  1. Hi Ali,

    Is there a way to incorporate this to an environment where Azure AD Connect Sync is in play. I recently discovered a new email domain that I need to add as a secondary alias for users that are Azure AD synced.

  2. Hello Ali,

    I have a situation where i need to a email address and make it primary as well, can you please help me.
    I tried to modify your above script but getting below errors.
    Cannot process argument transformation on parameter ‘EmailAddresses’. Cannot convert value
    “System.Collections.Hashtable” to type “Microsoft.Exchange.Data.SmtpAddress”. Error: “Cannot convert hashtable to an
    object of the following type: Microsoft.Exchange.Data.SmtpAddress. Hashtable-to-Object conversion is not supported in
    restricted language mode or a Data section.”
    + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin…mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
    + PSComputerName :

  3. Hi, and great article.
    Can you specify to only add a secondary SMTP to room mailboxes? Just add “-recipienttypedetails roommailbox” on line 5?

    Thanks

    1. Hi Joachim,

      Glad that you like the article.

      If you only want to target room mailboxes, change it to the following:

      From:
      $Mailboxes = Get-Mailbox -ResultSize Unlimited

      To:
      $Mailboxes = Get-Mailbox -RecipientTypeDetails RoomMailbox -ResultSize Unlimited

  4. Hello Ali,
    Nice article. I was just wondering if you ever had to do this where you need add more than one secondary SMTP address and for example make one of the primary SMTP ? Thank you in advance.
    Best regards,
    Karol

  5. Hey there Ali,

    I was trying to use the script but get the following error:

    Adding USERNAME@domain.com to USERNAME Mailbox
    Cannot process argument transformation on parameter ‘Identity’. Cannot convert value “USERNAME” to type
    “Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter”. Error: “Cannot convert the “USERNAME” value of type
    “Deserialized.Microsoft.Exchange.Data.Directory.Management.Mailbox” to type
    “Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter”.”
    + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin…mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
    + PSComputerName : outlook.office365.com
    Cannot process argument transformation on parameter ‘Identity’. Cannot convert value “USERNAME” to type
    “Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter”. Error: “Cannot convert the “USERNAME” value of type
    “Deserialized.Microsoft.Exchange.Data.Directory.Management.Mailbox” to type
    “Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter”.”
    + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin…mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
    + PSComputerName : outlook.office365.com

    Any tips on what could be going wrong?

    Cheers,
    Henk

    1. Hi Henk,

      I redacted sensitive information from your comment.

      You are running the script against the Microsoft 365/Office 365 tenant, which is why you are getting this error.

      The Add-SMTP.ps1 PowerShell script was intended to work only for Exchange on-premises and Exchange hybrid environments. I updated the script and the article with extra information.

      It works now for Exchange on-premises, Exchange hybrid, and Exchange Online (Microsoft 365/Office 365).

Leave a Reply

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