skip to Main Content

Getting started with Office 365 PowerShell | Playing with the Get-mailbox on screen display output | Part 2#4

In the current article, we will review how to use the famous PowerShell “Get” command for “fetching” (Get) object/s and, display information about the objects.

Additionally, we will learn how we can “shape” or optimize the output that we get from the PowerShell “Get” command for our specific needs.

The Power of PowerShell | Article Series

The Power of PowerShell article series, including the following three articles:

Introducing the PowerShell “Get” command

The “Get” command output is based on a predefined output format (the information that is displayed on the screen). For example, when using the get command for displaying information about Mailbox, the information will be displayed in a table format, with a predefined “Table Header” (properties).

For the demonstration purpose, we will use the most basic PowerShell cmdlet:

Get-Mailbox

Example 1 – Display information about specific instance of object

To get information about a specific Mailbox, we will need to provide the Mailbox identity.
The syntax for the PowerShell command is:

Get-Mailbox <identity>

You can see in the screenshot that the result (output) displayed in a table format, with a predefined “Table Headers” (Object propriety) such as the Mailbox Name and Alias, the Server name that hosts the Mailbox and the Mailbox Size.

Get cmdlets - Format output

Example 2 – Display information about object class

When we use the Get-Mailbox command without specifying the Mailbox identity, the “Get” command default, is to Get/Fetch all of the Mailbox class objects. (PowerShell “understand” that we need information about all of the objects that “belong” to the specific class).

To display a list of all the existing mailboxes, we use the command:

Get-Mailbox

In the screenshot you can see that the output from the Get-Mailbox command displays a list of all existing Mailboxes.

Get cmdlets - Format output

Note: we can control the number of the mailboxes that will be displayed. In case that we have a many mailboxes, the “Get-Mailbox” command will not display all of the Mailboxes. To “enforce” PowerShell to display a “complete list” of all the Mailboxes we can add the parameter: ResultSize unlimited

PowerShell command syntax:

Get-Mailbox -ResultSize unlimited

Get cmdlets – Format output results

When we use “Get” command for displaying information about objects, there are two available format options:

  1. Table Format
  2. List Format

(The default display format for the “ Get” command is: Table Format).

1. Table format

The “Table Format” (described also as a tabular format), enable us to display information in an “easy to read” format.
Table Format based on “Table column” (object properties) and the object values that display “under” the Table Headers.

The main disadvantage of the Table format is the PowerShell console width. The “amount” of data that can display on the screen restricted to the width of the PowerShell console. PowerShell enables us to control the “design” of the table format by choosing the Table Headers ( the object properties) that displayed on the screen, and by using “Format parameters” that enable us to control parameters such as – Word wrap, Row width and more.

As mention before, when we use the “Get” command, the default display format is Table Format. When we need to customize the output of the “Get” command, we need to “add” the Pipe option to “inform” PowerShell, what is the required format and what are the required properties or design parameters that we want to use.

The cmdlets that we use for using Table Format is Format-Table, or we can use the shortened version: FT

For example: when we want to display information in table format we can use the following syntax:

Get-Mailbox |Format-Table

An additional option is to use the shortened version: FT

Get-Mailbox |FT

In the screenshot, we can see the list of users Mailboxes. Notice that the “Table Headers” are “pre-configured” by the “Get” Command default display option.

Get cmdlets - Format output results Table Format

Let’s see what happened if we use the Get-Mailbox command without the option of |Format-Table.

You can see that the output is identical, because as mentioned, the default format for the “Get” command is Format-Table.

Get cmdlets - Format output

2. List Format

The main advantages of using the “List Format” are that we can display all the available information (property) about the object. The List Format, is not “affected” by the “width limitation” of the PowerShell console because the information about the object (properties and values), is displayed as a “list” under the object name.

This format could sometimes consider as “disadvantage” because, when displaying about many objects, the information in List format is “Less easy to read” vs. Table Format.

PowerShell command syntax:

Get-Mailbox |Format-List

An additional option is to use the shortened version: FL

Get-Mailbox |FL

If you look at the screenshot, you can see that when we use the “Format-List” option, we get many additional data fields (properties), that wasn’t available to us when we use the Table Format.

Get cmdlets - Format output results LIST Format

Format the output of “Get” command

In this section we will learn how to “optimize” the output that displayed by the “Get” command.
The challenges we face are:

  1. How to display the information (properties) that are relevant to our needs As you remember, the Table format displays very limited information and the List Format display “too much information” that is not so easy to read.
  2. How to overcome the limitations of PowerShell console screen width (The PowerShell console has a specific width that limits the amount of data that can be displayed).

Let’s start to review the different solution that we can use for mediating this limitation.

Using the “Property” option

The “property” option enables us to choose properties that are relevant to our needs.
For example, we want to display information about Mailboxes. The information that we are interested in include the following properties: User Alias, User Display Name, and User Email Address.

To be able to “override” the default Property that displayed by the “Get” command, we can use the “property” option and specify the “property names” that we want to display.

The PowerShell syntax is:

Get-Mailbox |Format-Table -Property Alias, Displayname,EmailAddresses

We can use a shortened version without specifying the “property” option. When we use the option “Format-Table” or “Format-List”, and add properties names, PowerShell is “smart enough” to understand that these are the properties that should be displayed.

The shorten version of the PowerShell command will look like:

Get-Mailbox |FT Alias,Displayname,EmailAddresses

Note: In the next examples, we will use the shortened version

General instructions:

  • Property name – the property name should be written as “one word” without a space.
  • Separate the property name – when we write more the one property name, we should add the “comma“ character after each Property name. We can have the “space” character between the Property names, as long as we use the “comma“ character to separate each of the Property names.

In the following screenshot, we can see the output of the Get-Mailbox command that displays the properties that we choose:

Get-Mailbox command choose properties File Table

Notice that although we manage to display the required properties, we still have the problem of the “width” of the PowerShell console.

When displaying properties that include “Long Values” an optional solution is to use the option of the List format.

For example, let’s use the same command and use the Format-List option:

Get-Mailbox |FL Alias,Displayname,EmailAddresses
Get-Mailbox command choose properties File LIST

Using the “Wrap” option

Another option that we use for “dealing” with “Long values” such as: email address, is the “Wrap“ option. When we use the “wrap” option, properties such as Email address that include “long values” that reach the PowerShell console “width” limitation, will be “moved” to the next line, so we will be able to see the “Full value”.
For example: to mitigate with the “PowerShell console width limitation”, we can use “Wrap” option.

PowerShell command syntax:

Get-Mailbox |FT Alias,Displayname,EmailAddresses –Wrap

In the screenshot, you can see the result when using the “Wrap” option. When the characters are “reach the end” of the PowerShell console right “border”, the rest of the “value string” (Email Address) will be displayed on a new line.

Get cmdlets - Format output results -WRAP

Using the “Group” option

The “Group” option, as the name implied, enable us to group the displayed results.
For example: when we use the standard “Get-Mailbox” command, the displayed result will include a list of all the Mailbox’s type such as a User Mailbox, Room Mailbox, Shared Mailbox, etc.

When using the “Group” option, we will need to specify the properties that will serve as the “source” for the “grouping”.

For example: for displaying information about Mailboxes grouped by their Mailbox type, we will use the property “RecipientTypeDetails”.

PowerShell command syntax:

Get-Mailbox |Group RecipientTypeDetails

In the screenshot you can see that the “Group” option enable us to get a “Clear View” about the Mailbox type that we have and about the number of each of the Mailbox’s types.

Get cmdlets - Format output results -GROUP

Using the “Sort” option

The “sort” option enables us to sort the output by relating to a specific Table Header (property).
For example: when we want to sort the output of the Get-Mailbox command by the property of “Alias”.

PowerShell command syntax:

Get-Mailbox |Sort-Object Alias

We can use a shortened version of the sort command:

Get-Mailbox | Sort Alias

The “sorting” of the values, could be by implemented by using Ascending or Descending order. There is no “Ascending switch” for the Sort-Object cmdlet, because the default of the “Sort” option is Ascending.

In case that we want to use the option of Descending, we can use the following syntax:

Get-Mailbox |Sort Alias Descending
Get cmdlets - Format output results -SORT

Using the “AutoSize” option

The option of “AutoSize” is relevant when we choose to display information by using the Table Format. As we know, when using the Table format, the output from the “Get” command is using a predefined “design parameters” such as the Table row width.

For example: when using the Get-Mailbox command, you can see in the screenshot, that Mailboxes with a “Long name” such as the “DiscoverySearchmailbox” are not fully displayed:

Get cmdlets - Format output results - Without AutoSize

To overcome this limitation, we can use the option of: -AutoSize
When using the AutoSize option, PowerShell will customize the Table row width to fit the value with the “Longest name”.

PowerShell command syntax:

Get-Mailbox |FT -AutoSize

Notice that now we can see the “Full name” of the “DiscoverySearchmailbox” but, because of the PowerShell console width limitation, other default properties were “removed” from the display, because the PowerShell console is not wide enough for displaying the rest of the properties such as server name, etc.

Get cmdlets - Format output results - AutoSize

Combination of display parameters

We can use a combination of display parameter to optimize the displayed output.
In the following example, we use a combination of “display parameter”: Sort, Format-Table and Groupby.

Note: instead of using the “group” parameter we will use the “Groupby” parameter. The option of: ”Groupby” enables us to display the information in different format.

PowerShell command syntax:

Get-Mailbox |Sort RecipientTypeDetails |FT Name,Alias
-Groupby RecipientTypeDetails
  • The first part will “Get” information about the entire existing Mailbox’s.
  • The second part will sort the result (the information about the Mailbox’s) by using the “RecipientTypeDetails” property.
  • The third part will format the output by using the Table Format, and display the “Alias” and the “name” property, and the option of “Groupby” will display the information in “sections”.

Each of the “Sections” includes information about Mailboxes that “belong” to specific Mailbox type such as a User Mailbox, Shared Mailbox, etc.

Get cmdlets - Format output results -GroupBy
The o365info Team

The o365info Team

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

This Post Has 0 Comments

Leave a Reply

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