If you want to sort Exchange and Office 365 mailboxes by size in a remote PowerShell session, you will face the problem that Get-Mailbox returns mailbox sizes in different units (KB, MB, GB). In this post I show how to convert the sizes to raw bytes so you can easily find the biggest mailboxes in your organization.
Latest posts by Jeff Brown (see all)

One common Exchange administrative task is to find the total size of mailboxes in the environment. This can be useful to find the biggest mailboxes or ones that are (nearly) empty as targets for removal due to inactivity. However, mailbox sizes are only available via PowerShell, and depending on where you run the Get-MailboxStatistics command, the options for sorting the output may require additional work.

First, what exactly is the problem with the default output? When viewing the size of multiple mailboxes through the TotalItemSize property, each mailbox does not have a standard unit. Instead, it converts mailboxes to a byte unit that best represents their sizes, like so:

Get MailboxStatistics default formatting

Get MailboxStatistics default formatting

We have different mailboxes with KB, MB, and GB listed. If you tried to sort the mailboxes by their TotalItemSize, the Exchange PowerShell module could do so, but if you exported this data to Excel, you couldn't sort it.

So what are the options if you need to export the data to another format? The Exchange PowerShell module also includes the ability to convert these values to a common byte unit. The TotalItemSize has built-in methods to convert to bytes, kilobytes (KB), megabytes (MB), gigabytes (GB), and terabytes (TB). You can view these methods by passing the value of TotalItemSize to Get-Member:

Available members, properties, and methods for Get MailboxStatistics

Available members, properties, and methods for Get MailboxStatistics

To apply these methods to the TotalItemSize property and display the data along with other mailbox statistics, PowerShell allows creating a calculated property in the output. This lets you create a custom property name and use an expression to display its value. Here are some examples for a single mailbox and multiple mailboxes. Notice that in the Expression property, the ToMB() method serves to calculate each mailbox's size to megabytes:

Get-MailboxStatistics -Identity gglass | Select-Object DisplayName, @{Name="TotalItemSizeMB";Expression={$_.TotalItemSize.Value.ToMB()}}, ItemCount
Get-Mailbox | Get-MailboxStatistics | Select-Object DisplayName, @{Name="TotalItemSizeMB";Expression={$_.TotalItemSize.Value.ToMB()}}, ItemCount
Examples of calculated properties

Examples of calculated properties

Now this is great if you run these commands in an Exchange PowerShell module window. But these methods are not available when using a remote PowerShell session connected to the Exchange server (for example, this would apply to Exchange Online in Office 365). The command will not return an error, but the calculated property would simply be blank.

In this case, we would need to manipulate the TotalItemSize value manually to extract the raw bytes in the string and convert them to the desired byte unit. The calculated property formula I have always used comes from this Scripting Guy! blog post from Brian Jackett. Here is an example of the formula he uses in a calculated property:

Get-MailboxStatistics -Identity gglass | Select-Object DisplayName, @{Name="TotalItemSizeMB"; Expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),0)}}, ItemCount

Let's break out his conversion:

[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),0)

First, he converts the TotalItemSize property to a string using a built-in method on the TotalItemSize property. Then he splits it on any opening parentheses and keeps the element at index 1. At this point, this is everything after the first parentheses in TotalItemSize:

Manual string conversion of TotalItemBytes

Manual string conversion of TotalItemBytes

Next, he splits the string again on any spaces and keeps the element at index 0. This should get just the number portion of the string. After that, he removes any commas:

Getting the number portion of the string

Getting the number portion of the string

Finally, he converts the byte count to megabytes by using the built-in PowerShell converter /1MB. Additional, he uses the .NET framework method [math]::Round to round the value to an integer with the option to specify the number of decimals to use (in this case, "0" is specific to give a nice round number):

Getting the output in megabytes

Getting the output in megabytes

The final output then looks like this:

Subscribe to 4sysops newsletter!

Final output using manual string conversion

Final output using manual string conversion

While a little complicated and messy, this example does show the power of what's possible using different methods to manipulate strings into meaningful values. Now we can export this to CSV or a similar format and use the calculated property TotalItemSizeMB to sort the values as needed.

5 Comments
  1. Javier 5 years ago

    Hi.

    I have to move my 60 mailbox (from Office 365 online) to another Office 365 tennant.

    Could you help me?.

    Greetings.

  2. Joe G. 2 years ago

    Are you aware of a similar method for Get-MailboxFolderStatistics? I would often use this command searching for FolderSize to get a list of folder sizes for users to determine where the bulk of their emails are located. 

  3. K H 1 year ago

    I am sorry to vent my frustration here, but to me what this shows in the first place is that some (many/most??) employees at Microsoft do not have the slightest idea how to provide good APIs/data structures.
    Who came up with the idea to NOT show the size in bytes? If I use Powershell I write programs/scripts and need to compare basic numbers and not human-readable text strings. If I need those, I provide them myself, thank you.

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account