'Deep Dive' into Office 365 PowerShell cmdlets: Get-MailBox

As powerful as Office 365 is, it can be made even more powerful with simple PowerShell cmdlets that can streamline your work and make your life easier. In today’s “Deep Dive,” we take an extended look into using PowerShell to collect mailbox information from Exchange Online.

In the previous part of this article series, we gave you an overview of Get-MailBox cmdlet and a few examples to get you started using the cmdlet to collect mailbox information from Office 365 Exchange Online. In this part and subsequent parts, we will look at some useful parameters of the Get-MailBox cmdlet and how you can use these parameters to get the desired information without spending much time with scripting.

Get-MailBox cmdlet and parameters

As we mentioned earlier, Get-MailBox cmdlet is available in both the on-premises Exchange Server installation and Exchange Online service. However, some parameters of Get-Mailbox cmdlet are not available to one environment or the other. For example, parameter ReadFromDomainController is only available in on-premises Exchange Server installation. Similarly, IncludeInactiveMailbox is also only available on-premises.

“Identity” parameter

“Identity” is the most common parameter that you will be using with Get-MailBox cmdlet. By using this parameter, you are instructing Get-MailBox to look for a mailbox that matches the given identity. For example, you can use “Email Address” with “-Identity” parameter to get the mailbox information of a user whose email address matches the email address you specified in the “-Identity” parameter. For example, to get mailbox information for johndthomas@TechGenix.com, you will execute the below PowerShell command:

Get-MailBox –Identity JohnDThomas@TechGenix.com

This command gets mailbox information by querying Exchange Online for an email address that matches with the given email address. Get-MailBox cmdlet supports other values to be used with the “-Identity” parameter as explained in the table.

Identity Value Remark
Name Specify the mailbox name of the user.
Display Name Need to specify the display name of the Office 365 user.
Alias Use Mailbox alias. In most cases, Alias value is similar to name value.
Distinguished Name Provide the distinguished name of the user.
Email Address As explained in an example earlier, use the email address of the user.
GUID Specify the GUID of the Office 365 user.
SamAccountName SamAccountName of the user.
Office 365 User ID As the name suggests, specify the Office 365 user ID for which you want to retrieve the mailbox information.
User Principal Name or UPN This is the UPN of the Office 365 user.

While you can use the above PowerShell command to retrieve the mailbox for an individual identity, if you need to retrieve mailbox information for multiple users, you need to use ForEach Loop in a PowerShell script. Let’s say you need to collect mailbox information for 200 email addresses. While you can use the above PowerShell command to retrieve mailbox information for each user one by one, it is going to take a considerable amount of time if you need to perform the task for 200 users manually. What you can do is create a text file, add all email addresses to the CSV file, and then use the below PowerShell script to collect required information. Note that when you run the Get-Mailbox command, it will show the default mailbox information, such as name, alias, server name where the mailbox is hosted, and other information. You might not want to see all information if you are looking for something specific. For example, if you need to know the “Alias” name for 200 mailboxes, all you need to do is use an “IF” condition in the PowerShell script as shown in the script below:

$MailBoxAliasReport = "C:\Temp\MailBoxAliasReport.CSV"

Remove-item $MailBoxAliasReport -ErrorAction SilentlyContinue

$STR = "Email Address, Name, Current Alias"

Add-Content $MailBoxAliasReport $STR

$EmailAddressFile = "C:\Temp\EMailAddresses.txt"

$GCEmailAddresses = GC $EmailAddressFile

ForEach ($Item in $EmailAddressFile)


$ThisEmailAdd = $Item

Write-Host "Processing Email Address : " $ThisEmailAdd

$MailBoxInfo = Get-MailBox -Identity $ThisEmailAdd | Select-Object Name, Alias

$STRNew = $ThisEmailAdd+","+$MailBoxInfo.Name+","+$MailBoxInfo.Alias

Add-Content $MailBoxAliasReport $STRNew


Write-Host "All Email Addresses from file" $EmailAddressFile " file were checked."

Write-Host "Report is generated in $MailBoxAliasReport file."

Once you have finished executing the above PowerShell command, a CSV report will be generated under “C:\Temp\MailBoxAliasReport.CSV” file, which contains the required information as shown in the screenshot below.

As you can see, the PowerShell script collected required information only for the email addresses that were specified in the C:\Temp\EmailAddresses.txt file. The ForEach loop command in the above PowerShell script hits only the required email address, collects the current alias of the mailbox user, and then stores the value in the report file.

In case you would like to retrieve other mailbox information, such as display name, server name, and mailbox size, you need to know the required attribute. You can easily get the supported attributes by using the Get-Member cmdlet.

Coming up

This article provided an overview of the “Identity” parameter of the Get-MailBox cmdlet. We also provided a PowerShell script that you can use to collect mailbox information for multiple Office 365 users. Coming up, we will continue to explain the parameters supported by the Get-Mailbox cmdlet and also explain the attributes supported by the Get-MailBox cmdlet.

If you would like to read the other parts in this article series, here are the links:

Part 1, Part 2, Part 3, Part 4, Part 5, Part 6, Part 7, Part 8, Part 9, Part 10, Part 11, Part 12, Part 13.

Nirmal Sharma

Nirmal Sharma is a MCSEx3, MCITP and was awarded the Microsoft MVP award in Directory Services and Windows Networking. He specializes in Microsoft Azure, Office 365, Directory Services, Failover Clusters, Hyper-V, PowerShell Scripting and System Center products. Nirmal has been involved with Microsoft Technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites.

Published by
Nirmal Sharma

Recent Posts

Azure DevOps Wiki: Manage your project documentation and collaboration

Not being able to find project documentation is way too common. Use Azure DevOps’ built-in…

2 days ago

Samsung Unpacked 2020: Galaxy S20, Galaxy Z Flip, and more

Samsung is again the first major company to roll out new smartphones in the new…

2 days ago

PhotoSquared data leak exposes users’ photos, information

PhotoSquared has experienced a data leak, mainly because the popular U.S.-based photo app failed to…

2 days ago

Moving data from an Azure VM to Storage Account with AzCopy

Here’s an elegant and modern way to move data from your Azure virtual machine to…

3 days ago

A lot not to like: Analysis of recent Facebook data breach

The effects of the recent Facebook data breach are still being felt. In this new…

3 days ago

Exchange 2019: Building an environment from scratch

Are you finally ready to take the plunge into Exchange 2019? If you are building…

3 days ago