Office 365 Exchange PowerShell vs On Prem PowerShell

Some things are the same – some most definitely not when running PowerShell scripts against Exchange in the cloud or on premises.

If you wanted a list of all mailboxes, sorted by biggest descending, this would do it:

Get-Mailbox | Get-MailboxStatistics | Sort-Object TotalItemSize -desc | ft DisplayName, ItemCount,TotalItemSize -a

For some reason, “TotalItemSize” is different in Office 365:

PS C:\tools> $me = get-mailbox fermin | Get-MailboxStatistics
PS C:\tools> $me.TotalItemSize

IsUnlimited Value
----------- -----
 False 4.775 GB (5,126,904,458 bytes)

If we look at the “Value” part of “TotalItemSize”, we get:

PS C:\tools> $me.TotalItemSize.value
4.775 GB (5,126,904,458 bytes)
PS C:\tools>

Since all we need is the value in Bytes, we can split this up:

  • First split is by the opening bracket “(“
  • With the second element [1] of the first split, we split by a space ” “
  • Use the first element [0]
  • This results in the string “5,126,904,458”, in which we get rid of the commas by replacing them with nothing
  • In the end divide by “1GB” to get mailbox sizes by gigabyte
PS C:\tools> ($me.TotalItemSize.value.ToString()).split('(')[1].split(' ')[0].replace(',','')/1GB
4.77480185963213

Round it to two decimal places:

PS C:\tools> [math]::Round(($me.TotalItemSize.value.ToString()).split('(')[1].split(' ')[0].replace(',','')/1GB,2)
4.77
PS C:\tools>

In conclusion:

Exchange 2016 (and 2013, 2010), on prem:

Get-Mailbox | Get-MailboxStatistics | Sort-Object TotalItemSize -desc | ft DisplayName, ItemCount,TotalItemSize -a

Office 365 Exchange:

Get-Mailbox | Get-MailboxStatistics | Select-Object DisplayName,ItemCount,@{label='Size (GB)';expression={[math]::Round(($_.TotalItemSize.Value.ToString().split('(')[1].split(' ')[0].replace(',','')/1GB),2)}} | sort 'Size (GB)' -Descending | ft -a

Looking forward to how this will be in Exchange 2019 on prem…

 

Leave a Reply

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