Skip to content

Remove Email addresses using PowerShell | Office 365 | Part 12#13

In this article, we review the subject of removing (deleting) E-mail address by using PowerShell. The subject of – “removing existing E-mail address” using PowerShell can cause some confusion because there are two types of “deletion operations” that depend on the specific PowerShell command syntax that we use.

The two methods for removing (deleting) E-mail address using PowerShell

The task of deleting the email address using PowerShell can be implemented in two ways:

  1. Replacing E-mail address with another E-mail address.
  2. Deleting E-mail address from existing E-mail addresses “pool.”

1. Replacing E-mail address with another E-mail address

The first method which I described as “replacing” existing E-mail address with a New E-mail address, can be a bit tricky.

When using, this option the NEW E-mail address “overrides” existing E-mail address (deleting existing E-mail address) and replace them with the NEW E-mail address that we provide.

The rules of – “deleting existing E-mail address” are not so clear because in this scenario the operation of “deleting E-mail address” Is not created on purpose.

When we use the PowerShell cmdlet Set-Mailbox, and provide the “NEW E-mail address” that we want to add to specific Exchange Online recipients, PowerShell will remove any existing E-mail address and then add the NEW E-mail address.

For example, in an Office 365 environment, I experienced a significant number of times, a scenario in which the PowerShell command that adds the NEW E-mail address, delete even non-SMTP E-mail address such as SIP address and in addition the “onMicrosoft” built-in E-mail address.

To demonstrate this concept, let’s use the following example:

In the following screenshot, we can see the E-mail addresses of an Exchange Online recipient
named – Christina.

Christina has 1 Primary E-mail address + 2 Alias E-mail addresses.

Replacing E-mail address with other E-mail address -01

We want to replace the primary E-mail address of Christina to the following E-mail
address – Christina-NEW01@o365info.com

To replace the existing primary E-mail address, we use the following PowerShell command:

Set-Mailbox Christina -EmailAddress Christina-NEW01@o365info.com

In the following screenshot, we can see the result:

The NEW E-mail address replaces the existing primary E-mail address and saves the previous primary E-mail address as – Alias E-mail address.

Notice that the Alias E-mail address was removed!

Replacing E-mail address with other E-mail address -02
2. Deleting E-mail address from an existing E-mail addresses “pool”

The second method for removing E-mail address (which I prefer to use), is implemented by using the PowerShell parameter @{remove.

When we use the PowerShell parameter @{remove, we “tell” PowerShell what the exact E-mail address we want to remove (delete).

To delete a specific E-mail using the PowerShell parameter @{remove, we need to use the following PowerShell command syntax.

PowerShell command syntax:

Set-Mailbox <identity> -EmailAddresses @{remove="<E-mail address>"}

PowerShell command example:

Set-Mailbox Christina -EmailAddresses @{remove="Christina-Alias02@o365info.com"}

To demonstrate this concept, let’s use the following example:

In the following screenshot, we can see the E-mail addresses of an Exchange Online recipient named – Christina.

Christina has 1 Primary E-mail address + 2 Alias E-mail addresses.

Deleting E-mail address from existing E-mail addresses using @ remove-01

We want to remove the following E-mail address – Christina-Alias02@o365info.com from Christina’s mailbox.

For this purpose, we use the following PowerShell command:

Set-Mailbox Christina -EmailAddresses @{remove="Christina-Alias02@o365info.com" }

In the following screenshot, we can see the result, the Alias E-mail address was removed, and all the rest of Christina E-mail addresses did not change in any way.

Deleting E-mail address from existing E-mail addresses using @ remove-02

You can read more information about the “behavior” of the PowerShell cmdlet – Set-Mailbox when adding E-mail address in the article Adding Email addresses using PowerShell | Office 365 | Part 3#13.

Quick tip

Another syntax option that we can use for removing E-mail address is implemented by using the “minus” () characters instead of the term “remove.”

We will need to use to enclose the minus character in quotes.

For example:

Set-Mailbox Christina -EmailAddresses @{"-"="Christina-Alias01@o365info.com" }

Remove all E-mail addresses that have a specific domain name | Bulk mode.

In this section, I would like to review a scenario, in which we need to remove (delete) E-mail address that has some specific characters such as – Domain name suffix.

This operation in which we address many “objects” (Exchange Online recipients) at the same time and perform a specific update, such as – removing the E-mail address, described as “Bulk.”

Before we continue with the exact PowerShell syntax, it’s very important for me to “warn you” because when using the option of “Bulk” and especially when relating to the task of – deleting E-mail addresses, we should plan carefully our steps, test the result of the PowerShell bulk command, use the option of Whatif and so on.

You can read more information about the “WhatIf” option in the article Adding Email addresses using PowerShell – Bulk mode | Office 365 | Part 4#13.

Scenario description

Our company owns two public domain names: o365info.com and o365pilot.com.
Following a legal problem, we will need to remove the domain name – o365pilot.com and deleted each of the E-mail addresses that their suffix includes the domain name – o365pilot.com

The characters of our organization E-mail address infrastructure are:

  • Each of the organization recipients has a primary E-mail address that uses E-mail address based on the domain name o365info.com.
  • Each of the organization recipients has an additional Alias E-mail address that has the “other” company domain name such as –o365pilot.com

We need to “write” a PowerShell bulk command that performs the following tasks:

  1. Run a Loop process on the existing Exchange Online recipient.
  2. Search and find an E-mail with a specific domain name suffix.
  3. Delete this E-mail address.

Let’s use the following scenario:

The name of the domain that we want to remove is configured via the variable $RemoveDomain.

The information about all existing Exchange Online mailboxes will be saved in the variable named $AllMailboxes.

The variable $AllMailboxes content is created by using a PowerShell query that looks for all the recipients who have an E-mail address that include the domain name – o365pilot.com.

Then we run a PowerShell ForEach statement that will loop via the Exchange Online mailboxes array and run the following tasks on each of the Exchange Online mailboxes that appear in the array:

  • Verify if the Exchange Online mailboxes have an E-mail address with the domain name suffix – o365pilot.com.
  • If the answer is “Yes” remove each of the E-mail addresses that include the domain name – o365pilot.com.
  • “Jump” to the next Mailbox in the list.

PowerShell command example:

$Domain = "o365pilot.com"
$RemoveSMTPDomain = "smtp:*@$Domain"

$AllMailboxes = Get-Mailbox | Where-Object { $_.EmailAddresses -clike $RemoveSMTPDomain }

foreach ($Mailbox in $AllMailboxes) {

    $AllEmailAddress = $Mailbox.EmailAddresses -cnotlike $RemoveSMTPDomain
    $RemovedEmailAddress = $Mailbox.EmailAddresses -clike $RemoveSMTPDomain
    $MailboxID = $Mailbox.PrimarySmtpAddress 
    Set-Mailbox "$MailboxID" -EmailAddresses $AllEmailAddress #-whatif

    Write-Host "The following email address where removed $RemovedEmailAddress from $MailboxID Mailbox"
}

In the next article, we will look at How to use the PowerShell script – manage Email addresses in Office 365 | Part 13#13.

o365info Team

o365info Team

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

This Post Has One Comment

Leave a Reply

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