Skip to 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

*Change the cmdlets in the script from Get-Mailbox to Get-RemoteMailbox and Set-Mailbox to Set-RemoteMailbox.

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.DistinguishedName -EmailAddresses @{remove = $_ } -WhatIf

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

Stop-Transcript
  • Line 11: 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 www.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.DistinguishedName -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:

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 C:\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 21 Comments

  1. Really appreciate the many guides you’ve created Ali. Helped me tremendously with my hybrid migration project. Thank you.

  2. Hello Ali,
    If we want to remove secondary SMTP address from specific bulk users(Csv file) what should be the script?

  3. Hi,

    Thanks for the script, it did the job for the most part but i stil have a large number of accounts which have an alias with the domain i’d like to remove. All these accounts don’t have a mailbox. What adjustments to the script are required for it to include all “User” accounts, even ones that don’t have mailboxes?

    Thanks,
    Ip.

  4. Thanks a lot, for $Mailbox.EmailAddresses | Where-Object { ($_ -clike “smtp*”) -and ($_ -like “*tajran*”) }

    I know -like, but what is -clike? I cannot understand it, could you please share some explanation on it, thank you so much !!!

  5. Hi Ali, will this script work for Exchange 2010? We are in the process of using Hybrid configuration to migrate from Exchange 2010 to Office 365 but first need to remove a specific email domain from all 2000 user mailboxes.

    1. It should work.

      A -WhatIf parameter is added to the script, so nothing will happen to the configuration when you run the script. Once satisfied with the results, remove the -WhatIf parameter and run the script.

  6. Hi Ali, can you tell us how to remove SMTP address from a specific OU and not the whole organization.

    1. Get the OU distinguishedName and follow the below steps:

      Change lines 4-5 in the script:

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

      With the below lines:

      # Fill in OU
      $OUScope = "OU=IT,OU=Users,OU=Company,DC=exoip,DC=local"
      
      # Get all mailboxes in OU
      $Mailboxes = Get-Mailbox -OrganizationalUnit $OUScope -ResultSize Unlimited
  7. Is it possible to remove multiple email addresses at the same time, migrating public folders to EOL and microsofts script allowed it to create the mail enable object in EOL with multiple domains that no longer exist, your script worked perfectly on the on premise exchange to remove the obsolete domains, but EOL it wont allow the commit it still errors saying

    “You can’t use the domain because it’s not an accepted domain for your organization.”

    i believe this is due to it unable to update the object because there is still other domains that dont exist as excepted domain in EOL

    your help would be much appreciated

  8. 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?

  9. 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?

  10. 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).

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

  12. 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 *