Calculate Exchange & O365 Mailbox Size with Regex

While Get-MailboxStatistics can quickly show total mailbox size the format is terrible and calculated on the fly resulting in something like this:


I used to throw multiple split commands at it to get the byte count, and while that worked, it was ugly.¬† I’ve been on a regex kick recently and simplified the solution a bit:

   Convert size attributes pulled from Get-MailboxStatistics to bytes only
   Exchange and Office 365 Get-MailboxStatistics command can return size of targeted mailbox but it is not
   useful when attempting to sort after exporting.  This function can be used to parse the provided string value
   and return a type [double] that can be used for sorting
   Convert-MailboxSizeToBytes ((Get-MailboxStatistics '').TotalItemSize)
   Convert-MailboxSizeToBytes ((Get-MailboxStatistics '').TotalDeletedItemsize)
function Convert-MailboxSizeToBytes
    [ValidateScript({$_ -match '.*\([\d,]+\Wbytes\)$'})]
  write-debug "$($MyInvocation.InvocationName):  value = $value"
  if($value -match '(?<=\()[\d,]+'){[double]$matches[0]}else{$null}


Once you get the results back, calculate the sum, divide by your favorite built-in denominator (1KB, 1MB, 1GB), tack on as a noteproperty, and export the results for simple sorting.