Manage Email addresses using PowerShell | Office 365 | Command reference | Part 2#13 5/5 (3) 18 min read

The current article is the continuation of the former article in which we review various management tasks that relates to the subject of – Managing E-mail address using PowerShell.
In this article, we review that following types of management tasks:

  • Display information about E-mail addresses.
  • Searching for information about E-mail addresses.
  • Removing (deleting) E-mail address using PowerShell.


Display information about E-mail addresses

The E-mail address “store” | EmailAddresses property

The information about mailbox recipient E-mail address such as – Primary E-mail address, Proxy E-mail address, SIP address and x500 address, is “stored” the mailbox property
named – EmailAddresses.

The need to “manipulate” the data about E-mail address

Technically, the task of – displaying or exporting information about the E-mail addresses of a recipient, can be considered as a simple task.
However, in many scenarios, we will need to “adjust” the information that the “standard” PowerShell command provides. For example,

Issue 1 – our ability to select a specific “piece of information” from the “E-mail address Array” that is stored in the EmailAddresses.

By default, PowerShell “spit” all the content that is stored on the EmailAddresses. Exchange Online PowerShell cmdlets, doesn’t include a built-in parameter that enables us to filter the information such as asking to display only the Proxy E-mail address and so on.

Issue 2 – the PowerShell defaults will display the content of the EmailAddresses property with additional “peace of information” that are associated with the “original information E-mail address” such as the “smtp:” prefix.

We will start with the basic PowerShell syntax we use for “fetch” information about E-mail address, and in the next section, we review how to use Hash table, and Custom expressions for – optimizing and adjusting the information that we need to get the recipient E-mail address.

View information about E-mail address

The “default” PowerShell command syntax that we use for – display information about a recipient E-mail address is quite simple.

We need to specify:

  • The specific type of recipient such as “mailbox recipient”.
  • The identity of Exchange Online recipient such as Alias, E-mail address etc.
  • The specific properties that we want to display such as – DisplayName, PrimarySmtpAddress, EmailAddresses.

PowerShell command syntax

PowerShell command example

Display information about Primary E-mail address

In case that we need to display information only about the Primary E-mail address of specific Exchange Online recipient, we can use the following syntax:

PowerShell command example

To display information about all Exchange Online mailboxes Primary E-mail address, we can use the following PowerShell command syntax:

PowerShell command example

Expend the “E-mail address property”

Regarding the “issue” of displaying the “Full content” the multiple value recipient E-mail property, we can implement a “partial solution” for a specific recipient for the issue of displaying multiple E-mail addresses, but this solution is not properly implemented in a scenario in which we need to display information about multiple recipients

In the following example, we use a PowerShell command that gets information about
Angelina E-mail addresses.

PowerShell command example

In the output, we can see that the “EmailAddresses” property includes many E-mail addresses.

Optimize the displayed results about – recipient E-mail addresses using Hash table

In this section, we review the how to use Hash table and Custom expressions, as a “tool” that enhances our capabilities for digesting the “native” information that we get from PowerShell about recipient E-mail address.

In our specific scenario, we use the PowerShell Hash table for three main purposes:

1. Get information about specific types of E-mail address

As mentioned, by default PowerShell displays all the content of the EmailAddresses property. By using the option of Hash table and Custom expressions, we can create a new custom “recipient property” that enables us to use a filter that gets information only about specific E-mail address such as Proxy E-mail address or SIP address.

2. Optimizing the PowerShell output of recipient E-mail address | Removing “unnecessary data” from the output.

By default, PowerShell adds information about the “type” of the E-mail address at the begging of the
E-mail address by adding a Prefix such as “smtp:”.
By using the option of Hash table and Custom expressions, we can define a “request,” that will remove the unnecessary prefix from the PowerShell command output.

The additional character of the default PowerShell command output is related to a scenario in which recipient has multiple E-mail addresses. In this case, PowerShell will display the information about the E-mail addresses sounded by “curly bracket.”

By using the option of Hash table and Custom expressions, we can define a “request,” that will remove the unnecessary curly bracket from the PowerShell command output.

3. Optimizing the PowerShell output of recipient E-mail address | Adding “separation character

When we export information about recipient E-mail address to a CSV file, PowerShell exports information about multiple E-mail addresses to “one row” and doesn’t use a “separation character.”

Instead, PowerShell adds a “space character” between each E-mail address. In case that we need to import information from the CSV file, we need to have a “separation character” that will enable the import process to distinguish between the different E-mail addresses.

By using the option of Hash table and Custom expressions, we can ask from PowerShell to add a “separation character” that will separate each E-mail address from the additional E-mail address.

A demonstration of the way we use the Hash table and Custom expressions

In the specific example, we use the PowerShell Hash table and Custom expressions for performing the following tasks:

  • Display information only about the proxy E-mail address (the proxy E-mail address starts with a smtp prefix written in small caps).
  • Remove the smtp prefix from the PowerShell output of the E-mail address.
  • Remove the “curly bracket” the surround the E-mail Address Array.
  • Add a separator character – in our example, we use the characters (;)

We use the following PowerShell command syntax:

In the following box, we can see the result of the PowerShell output.
Notice that now

  • Each E-mail address appears without the smtp prefix.
  • The “curly bracket” the surround the E-mail address array was removed.
  • Each E-mail address is separated by the characters – (;).

PowerShell console output example

Using Hash table for display information about – recipient E-mail address

In the following section, I would like to review a couple of examples to a scenario in which we use PowerShell Hash table, for getting information about specific Exchange recipient E-mail addresses.

Display only Primary E-mail address

The PowerShell command – Get-Mailbox, include a dedicated parameter that enables us to display information about a specific E-mail address that defines as – Primary E-mail address.

In other words, we don’t need to implement any sophisticated PowerShell command syntax such as using the Hash table for a display Primary E-mail address.

To be able to view only the Primary E-mail address, we can use the following PowerShell syntax:

Although we don’t need to use a special command configuration, I would like to provide an example to the Hash table syntax, that we use displaying E-mail that defined as a Primary E-mail address.

In this example, we define a condition using the “cmatch” logical operator, that “fetch” information about E-mail address only if the prefix of the E-mail address include the string “SMTP:” using capital letters.

PowerShell command example

Display only SIP address

In the current scenario, we want to view information only about recipient address, which described as “SIP address” (addresses that are used by Skype for business users).

SIP address begins with the prefix “sip:

To be able to “fetch” information only about SIP addresses, we define the following Hash table Expression:

PowerShell command example

Display only x500 E- address

In the current scenario, we want to view information only about E-mail address, which described as “x500 E-mail address”.

X500 E-mail address, begins with the prefix “x500:

To be able to “fetch” information only about x500 addresses, we define the following Hash table Expression:

PowerShell command example

You can find more detailed information about the subject of – Hash table and Custom expressions in the article – Display information about E-mail Addresses using PowerShell | Customizing the output using Hash Table and Custom expressions | Office 365 | Part 7#13


Searching for information about E-mail addresses

In the following section, we will demonstrate how to use the PowerShell “Where statement” for creating a custom search query, that will look for

  • A specific domain name suffix.
  • A specific E-mail address.
In case that you want to read more information about the PowerShell “Where” syntax that we use for creating a search filter, you can read the Article

In a scenario in which we need to “address” each of the different types of Exchange Online recipients, looking for a specific E-mail address, we need to know, what is the “magic” PowerShell cmdlets that will enable us to perform this task.

In reality, there is no such “magic PowerShell command” but instead, we can use a combination of three different PowerShell cmdlets, that will enable us to fulfill the requirement of – “addressing” each of the existing Exchange Online recipients.

In a scenario in which we need to perform a search that will “address” of the available Exchange Online recipients types, looking for E-mail addresses with a specific domain name suffix, we need to use a combination of the following three PowerShell commands:

  • Get-Recipient
  • Get-Mailbox -SoftDeletedMailbox
  • Get-UnifiedGroup
You can find more detailed information about the way that we perform search for a specific E-mail address or Office 365 UPN in the article – Searching Email addresses with a specific domain name suffix Using PowerShell | Office 365 | Part 10#13

Using a PowerShell script for performing a search of – E-mail address, with a specific domain name suffix

The task description

  • We need to perform a search that will locate all the Exchange Online recipients, who have E-mail address with a specific Domain name suffix.
  • We want that the PowerShell “results” will be saved in a CSV file.

The characters and the logic of the PowerShell script

1. The Domain name suffix we look for.
The PowerShell script will “ask” us about the specific Domain name suffix we look for. The information will be stored in a variable named – $DomainSuffix.

2. Exported file’s location
In our specific demonstration, we ask from PowerShell to create for us, a NEW folder using the following path: C:\INFO\E-mail address\Exchange Online\E-mail address with Domain suffix – $Domainsuffix

3. Exported files
As mentioned, to be able to get a “view” of all the existing Exchange Online recipients, we need to use three different PowerShell cmdlets –Get-Recipient, Get-Mailbox -SoftDeletedMailbox and Get-UnifiedGroup. The output from each command will be stored in a separated CSV file.

Searching “Hidden” Email addresses Using PowerShell

Searching for specific E-mail address | search scope = Exchange Online

The search addresses all existing Exchange Online recipients and searches for a
specific E-mail address E-mail address.

You can find more detailed information about the subject of searching for a specific E-mail address in the article – Searching “hidden” Email addresses Using PowerShell | Office 365 | Part 11#13

Removing (deleting) E-mail address using PowerShell

To be able to remove (delete) existing E-mail of Exchange recipient, we need to use the PowerShell method – “@{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

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.
You can find more detailed information about the subject of removing E-mail address in the article – Remove Email addresses using PowerShell | Office 365 | Part 12#13


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

The next article in the current article series

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
Manage Email addresses using PowerShell | Office 365 | Command reference | Part 2#13
Article Name
Manage Email addresses using PowerShell | Office 365 | Command reference | Part 2#13
Description
In this article, we review that following types of management tasks: Display information about E-mail addresses. Searching for information about E-mail addresses. Removing (deleting) E-mail address using PowerShell.
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 *