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:
<# .Synopsis Convert size attributes pulled from Get-MailboxStatistics to bytes only .DESCRIPTION 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 .EXAMPLE Convert-MailboxSizeToBytes ((Get-MailboxStatistics 'first.last@contoso.com').TotalItemSize) .EXAMPLE Convert-MailboxSizeToBytes ((Get-MailboxStatistics 'first.last@contoso.com').TotalDeletedItemsize) #> function Convert-MailboxSizeToBytes { [CmdletBinding()] Param ( [Parameter(Mandatory=$true,Position=0)] [ValidateScript({$_ -match '.*\([\d,]+\Wbytes\)$'})] $value ) 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.