skip to Main Content

Bulk remove secondary SMTP address with PowerShell

In the previous article, you learned how to list all SMTP addresses with PowerShell. We can see that the mailboxes have more than one SMTP address configured. These secondary SMTP addresses are also known as an alias address. In this article, you will learn how to bulk remove secondary SMTP address with PowerShell.

Introduction

The Remove-SMTP.ps1 PowerShell script works for:

  • Exchange on-premises
  • Exchange hybrid
  • Exchange Online

Prepare the remove SMTP address PowerShell script

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

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

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

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

    # Change @contoso.com to the domain that you want to remove
    $Mailbox.EmailAddresses | Where-Object { ($_ -clike "smtp*") -and ($_ -like "*@contoso.com") } | 

    # Perform operation on each item
    ForEach-Object {

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

        # Write output
        Write-Host "Removing $_ from $Mailbox Mailbox" -ForegroundColor Green
    }
}

Stop-Transcript

Change the @contoso.com value to the domain that you want to remove from the mailboxes.

A couple of examples:

  • Example 1: Remove only SMTP addresses with the domain alitajran.com. Add “@alitajran.com“.
  • Example 2: Remove all SMTP addresses with the name tajran. Add “*tajran*“.

In my example, this is how it looks.

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

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

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

    # Change @contoso.com to the domain that you want to remove
    $Mailbox.EmailAddresses | Where-Object { ($_ -clike "smtp*") -and ($_ -like "*tajran*") } | 

    # Perform operation on each item
    ForEach-Object {

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

        # Write output
        Write-Host "Removing $_ from $Mailbox Mailbox" -ForegroundColor Green
    }
}

Stop-Transcript

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

Bulk remove 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:

Exchange on-premises / Exchange hybrid: Run Exchange Management Shell as administrator.

Exchange Online (Microsoft 365/Office 365): Run PowerShell as administrator and connect to Exchange Online PowerShell.

Go to the script path and run the Remove-SMTP.ps1 script. The script will go through all the mailboxes in the Exchange Organization. Good to know is that 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, all the SMTP addresses containing tajran will be removed in bulk. After running the script, confirm that these are the SMTP addresses that need to be removed.

[PS] C:\>cd \scripts
[PS] C:\scripts>.\Remove-SMTP.ps1
Transcript started, output file is C:\temp\Remove-SMTP-Address.log
What if: Setting mailbox "Kyle Peake".
Removing smtp:Kyle.Peake@alitajran.com from Kyle Peake Mailbox
What if: Setting mailbox "Kylie Davidson".
Removing smtp:Kylie.Davidson@alitajran.com from Kylie Davidson Mailbox
What if: Setting mailbox "Larson Tevin".
Removing smtp:Larson.Tevin@alitajran.com from Larson Tevin Mailbox
What if: Setting mailbox "Madeleine Ross".
Removing smtp:Madeleine.Ross@alitajran.com from Madeleine Ross Mailbox
What if: Setting mailbox "Mary Walsh".
Removing smtp:Mary.Walsh@alitajran.com from Mary Walsh Mailbox
What if: Setting mailbox "Max Fraser".
Removing smtp:Max.Fraser@alitajran.com from Max Fraser Mailbox
Transcript stopped, output file is C:\temp\Remove-SMTP-Address.log

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

[PS] C:\scripts>.\Remove-SMTP.ps1
Transcript started, output file is C:\temp\Remove-SMTP-Address.log
Removing smtp:Kyle.Peake@alitajran.com from Kyle Peake Mailbox
Removing smtp:Kylie.Davidson@alitajran.com from Kylie Davidson Mailbox
Removing smtp:Larson.Tevin@alitajran.com from Larson Tevin Mailbox
Removing smtp:Madeleine.Ross@alitajran.com from Madeleine Ross Mailbox
Removing smtp:Mary.Walsh@alitajran.com from Mary Walsh Mailbox
Removing smtp:Max.Fraser@alitajran.com from Max Fraser Mailbox
Transcript stopped, output file is C:\temp\Remove-SMTP-Address.log

I hope that this helped you to remove unwanted SMTP addresses from the mailboxes in the Exchange organization.

Keep on reading: Find email addresses with PowerShell »

Conclusion

In this article, you learned how to bulk remove secondary SMTP address with PowerShell. Download the Remove-SMTP PowerShell script. Add the domain that you like to remove and run the script. The script output will show which proxy addresses are removed. 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 10 Comments

  1. Hi Ali,
    Thanks for sharing the wonderful script. Instead of running this script against all users, how can I import the list of mailboxes through csv and run it against them only?

  2. Hi Ali..when I use this script my sip address which is the same as as my domain name also gets removed..how to select only secondary smtp address alone?

  3. Hi Ali, can you make this work for Exchange Online? I tried but I can’t get it to work. Thank you a lot.

    1. The Remove-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).

  4. Thank you, this is exactly what I was looking for to remove domains from on prem mailboxes before our Exchange Online migration.

  5. Can you also make it work for remote mailboxes (Get-RemoteMailbox and Set-RemoteMailbox)?

Leave a Reply

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