In this article, we review that subject of – how to create a Hash table + Custom expressions as a “tool” that we can use for optimizing and manipulating the PowerShell output in scenarios of – Get information about recipient’s E-mail addresses.

We will learn how to create a Hash table + Custom expressions that performs the following tasks:

Using PowerShell Hash table basic introduction

The PowerShell option which describes as – “Hash table“, is a very powerful option, that enables us to create a “NEW property” that is customized to our specific needs.

The custom property can be implemented in various ways, such as – calculated field, search query and so on.

For example, the PowerShell command Get-Mailbox, can be used for displaying the information stored in the “EmailAddresses” property.

The “EmailAddresses” property includes a “list,” of all the available “address” that specific Exchange recipient has.

By default, the PowerShell command Get-Mailboxdoesn’t include a built-in parameter that enables us to “filter the search results,” and asks to get information only a specific type of E-mail address such as Proxy E-mail addresses.

The good news is, that by using the option of – PowerShell “Hash table,” we can create a “NEW recipient property,” that will include the specific information that we need.
In our case, all the Proxy E-mail addresses of a recipient.

The creation of this “NEW recipient property” is implemented by “Hash table”.

The structure of “Hash table” (custom information fields)

The concept of “Hash table” (custom property) is implemented by using a syntax that includes two parts:

Part 1#2 – The Label part

This is the first part of the Hash table, in which we define the “Name” of the custom property.
The “Name” is just an arbitrary name whom we define.

Part 2#2 – The Expression part.

The is the second part of the Hash table, in which we define the “Expression.”
The term “Expression,” can be translated into various types of “PowerShell sentences” such as – a Filtered search query, calculated value and so on.

“Hash table” | Naming convection

There are two options of naming conviction that we can use for the writing Hash table.

Option 1#2

The naming convention that we use for defining the label part, is implemented by using the
letter “L” (Label).

The naming convention that we use for defining the expression part, is implemented by using the
letter “E” (expression).

Option 2#2

The naming convention that we use for defining the label part, is implemented by using the term “Name”

The naming convention that we use for defining the expression part, is implemented by using the
term “Expression”

“Extract” Proxy E-mail address using “Hash table” example.

To demonstrate the way that we use Hash table (custom property), let’s use the following example: we want to display information about the Proxy E-mail address of the recipient.

In the following diagram, we can see the structure of the PowerShell command that we will use:

Part A – this is a standard PowerShell “Get-Mailbox” command, in which we ask from PowerShell, to get all the available information about the Exchange Online recipient mailbox.

Part B – In this part, we use the PowerShell parameter “select” for displaying only two properties of the recipients: the standard “DispalyName” property + NEW custom property that we define by using a Hash table.

The syntax of the Hash table

The syntax of the PowerShell Hash table that we use in our case for extracting information about
Proxy E-mail addresses is:

In the following diagram, we can see the different part of the Hash table:

Number 1 – we use the “@” character, for “declaring” that we create a special array (Hash table) that includes the Array name + PowerShell expression such as filtered search query, calculated value and so on.

Hash table – name

Number 2 – this is the “name” that we define for the Array. In our example, we define the Array name – “Proxy Email address.”
And again, this is just an arbitrary name whom I choose for my specific needs.

Hash table – Expression

Number 3 – in our example, we ask from PowerShell to create a “filtered search query,” that will be implemented in the following way:

We instruct PowerShell to query a recipient property named – “EmailAddresses”.
A quick reminder – this is the mailbox property, that store information about every existing E-mail address and other address.

Number 4 – we define a filter that will fetch information only about Proxy E-mail addresses.
The filter is based on the following structure:

We use the “Where-Object” PowerShell statement, to define a condition, that uses the logical
operator “cmatch“.

A quick reminder-

Exchange stores information about smtp E-mail address by using the “smtp:” prefix.

Proxy E-mail addresses are represented by the “smtp:” prefix written in small caps.

Primary E-mail address represented by the “SMTP:” prefix written in capital letters.

The condition that we define is actually based on two separated condition.

We ask from PowerShell, to “fetch” information only about E-mail address that starts with the prefix – “smtp:” and in addition, the smtp prefix must be written in small caps.

To fulfill this requirement, we use the PowerShell logical operator – “cmatch”.

PowerShell logical operator – “cmatch” was created for looking for a “perfect match” + performing a case-sensitive match.

In this section, we review the how to use Hash table and Custom expressions, for two main purposes:

Enhance our capability to create a custom\ new recipient property that will enable us to view information about specific types of E-mail address such as – Proxy E-mail address, SIP address, x500 E-mail and so on.

Optimizing the PowerShell output of recipient E-mail address by

Removing “unnecessary data” from the output

Adding “separation character,” that will enable us to work better with mailbox attribute that defined as multivalued attribute that contain multiple E-mail addresses.

In the former section, we review the way to create a PowerShell Hash table that “filter” the search result by displaying information about E-mail addresses that have a specific character such as Proxy E-mail addresses.

In the following diagram, we can see the “output” that we got from the PowerShell command.

The result looks “ok” because we got the required information meaning – the Proxy E-mail
addresses of Christina

Issue 1 – The curly brackets

When Exchange Online recipient has more than one Proxy E-mail address, PowerShell will display the information using a curly bracket. In many scenarios in which we need to “work” with the “E-mail address information”, we will need to remove the curly bracket characters.

Issue 2 – The separation characters

When PowerShell “see” more than one E-mail address, PowerShell will use the comma (,) character for separating between the values (E-mail addresses in our case).

The “thing” is that when we export the information to a file such as a CSV file, PowerShell will not use the comma (,) character, but instead, will use a “space” to separate the values.

In case that we need to “manipulate” the data stored in a CSV, we need to use some “separation character,” that will enable us to address separately each value.

Issue 3 – Smtp prefix

PowerShell displays each E-mail address using a prefix, that defines the type of the E-mail address. In our example, each proxy E-mail address will be displayed using the prefix “smtp:” using small caps.

In case that we need to “do something” with the information about the “E-mail address,” we need to use the “clean” from the E-mail address the “smtp:” prefix.

Optimize the output of the Hash table using the parameters- Replace + Join.

To be able to solve the mentioned “issues” we will use two PowerShell parameters

Replace

Join

In the next section, we will add the additional parameter to the PowerShell Hash table, that will help us to improve the output of the Hash table.

In the following diagram, we can see the additional parameters that were added to the “original syntax” of the Hash table.

Remove the smtp prefix | Using the Replace parameter

We will start with the mission of – removing the “smtp prefix,” from the information that is displayed about the Proxy E-mail addresses.

In our example, we use the following syntax -replace ‘smtp:’.

The purpose of the PowerShell command “replace” is, as the name implies, to replace an existing string with a “NEW string.”

In our example, we want to remove a string (“smtp prefix”), and not to “replace” the “smtp prefix.”

The trick that we use is, that we “tell” PowerShell, to replace the “smtp prefix” with “nothing.”
In other words, we ask from the PowerShell “replace” parameter, to replace the “smtp prefix” and replace this text string with “empty space.”

To be able to fulfill the requirement of – removing the “smtp prefix” from the PowerShell output
of the E-mail address, we use the following PowerShell command syntax:

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:

1

Get-MailboxBilly|SelectAlias,PrimarySmtpAddress

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.

An additional variation of this case is a scenario in which we want to get information about Exchange Online recipients whom they Primary E-mail address includes a specific domain name suffix OR “other” domain name suffix.

In our example the domain name suffix that we look for the recipient whom their Primary E-mail address includes the domain name suffix – o365pilot.com or o365info.com