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.

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 { $_.AddressString -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 -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 { $_.AddressString -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 -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

Run Exchange Management Shell as administrator. 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 0 Comments

Leave a Reply

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

Back To Top