Remove Email addresses using PowerShell | Office 365 | Part 12#13 5/5 (3) 8 min read

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 additional 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 – [email protected]

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

In the following screenshot, we can see the result:

The NEW E-mail address replaces the existing primary E-mail address and saves the former 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 is the exact E-mail address that 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

PowerShell command example

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 – [email protected] from Christina’s mailbox.

For this purpose, we use the following PowerShell command.

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

Note – 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:

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

Note – 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, have a primary E-mail address that uses E-mail address based on the domain name o365info.com.
  • Each of the organization recipients, have 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 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 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


Managing E-mail Addresses | PowerShell scripts series

The subject of “Managing E-mail addresses” in an Exchange environment, can be translated into various administrative tasks such as – Bulk Adding Alias E-mail address to multiple Exchange Online mailboxes, Bulk Removing Alias E-mail address from multiple Exchange Online mailboxes, backup existing E-mail address, replace primary E-mail address and so on.
To be able to accomplish these various tasks, I have created seven PowerShell menu scripts, that will help you in performing these tasks.

Export Email addresses | Exchange Online | Using PowerShell | 1#7

A PowerShell menu script, that will help you to:
Create a report (that will be exported to files) about – All Exchange Online recipients and their different type of E-mail address such as SMTP E-mail address (Primer E-mail address and Alias E-mail address), SIP address, X.500 E-mail address, etc.

You can read more detailed information about the PowerShell commands that are used in the script in the articles:

Export and display information about Email addresses using PowerShell - Office 365 - Part 6-13


Search Email Addresses + export | Exchange Online | Using PowerShell | 2#7

A PowerShell menu script that will help you to:
Perform a search for a specific E-mail address or, for an E-mail address that uses a specific Domain name suffix of Exchange Online recipients, and exports the information to files.

In case that you want to get more detailed information about the subject of – locating specific E-mail addresses, you can read the articles:

Searching hidden Email addresses Using PowerShell - Office 365 - Part 11-13


Bulk Add Alias E-mail address – Specific Domain name suffix | 3#7

A PowerShell menu script that will help you to:
TEST + implement the process of – Bulk add additional Alias E-mail addresses with a specific Domain name suffix, to multiple Exchange Online mailboxes.

Additional information

In case that you want to get more detailed information about the subject of – adding additional Alias E-mail address, you can read the articles:

Adding Email addresses using PowerShell - Bulk mode - Office 365 - Part 4-13


Add Additional Proxy (Alias) E-mail address | import from a CSV | 4#7

A PowerShell menu script that will help you to:
TEST + implement the process of – Bulk add an additional Proxy (Alias) E-mail addresses to the Exchange Online recipient, that are imported from a CSV file.

Additional information

In case that you want to get more detailed information about the subject of – Add additional Alias E-mail by importing the information from a CSV file, you can read the articles:

Adding Email addresses using PowerShell - Office 365 - Part 3-13


Bulk Remove Alias E-mail address – Specific Domain name suffix | 5#7

A PowerShell menu script that will help you to:
TEST + implement the process of – Bulk deleting existing Alias E-mail addresses with a specific Domain name suffix from multiple Exchange Online mailboxes.

Additional information

In case that you want to get more detailed information about the subject of – removing (deleting) E-mail address with a specific domain name suffix, you can read the article   Remove Email addresses using PowerShell | Office 365 | Part 12#130
Remove Email addresses using PowerShell - Office 365 - Part 12-13


Assign NEW Primary E-mail address using a specific Domain name suffix | 6#7

A PowerShell menu script that will help you to:
TEST + implement the process of – Bulk replacing (remove) existing primary E-mail addresses, with a NEW Primary E-mail address that has a specific Domain name suffix.

Additional information

In case that you want to get more detailed information about the subject of – replacing Primary E-mail address, you can read the article – Adding Email addresses using PowerShell | Office 365 | Part 3#13

Adding Email addresses using PowerShell - Bulk mode - Office 365 - Part 4-13


Manage E-mail addresses – Single mailbox | Exchange Online | 7#7

A PowerShell menu script that will help you to:
Manage E-mail address of a specific Exchange Online mailbox (add Alias, delete Alias, replace primary E-mail address).
Adding Email addresses using PowerShell - Office 365 - Part 3-13


Now it’s Your Turn!
It is important for us to know your opinion on this article

Restore Exchange Online mailbox | Article series index

Summary
Remove Email addresses using PowerShell | Office 365 | Part 12#13
Article Name
Remove Email addresses using PowerShell | Office 365 | Part 12#13
Description
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.
Author
Publisher Name
o365info.com
Publisher Logo

Related Post

Please rate this

Eyal Doron on EmailEyal Doron on FacebookEyal Doron on GoogleEyal Doron on LinkedinEyal Doron on PinterestEyal Doron on RssEyal Doron on TwitterEyal Doron on WordpressEyal Doron on Youtube
Eyal Doron
Share your knowledge.
It’s a way to achieve immortality.
Dalai Lama

Leave a Reply

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