Get mailbox data size and number of mailbox items for your tennant.

mailbox

Short little script to get the totalt amount of mailboxdata and number of mailbox items in your Office 365 tenant. Shure, this could easily been done in the Office 365 services. But whats the fun in that? I did it PowerShell to learn a thing or two on the way. I even got to use regex to extract a value from a string.

Just so you are warned. If you have alot of mailboxes, it will take some time to run this script.

$tenantinfo = @()
$users = Get-Mailbox | Get-MailboxStatistics | Select-Object TotalItemSize, ItemCount

ForEach($user in $users){

$bytesvalue = $user.TotalItemSize
$bytesvalue.value -match ".?\((.*?)\).*" | Out-Null
$bytesvalue2 = $Matches[1]
$bytesvalue2 -match "^.*(?=(\ bytes))" | Out-Null
$bytes = $Matches[0]
$object = New-Object psobject -Property @{
TotalSize = [int64]$bytes
TotalItems = [int64]$user.ItemCount
}
$tenantinfo+= $object
}

$sumsize = ($tenantinfo.TotalSize | Measure-Object -Sum).sum/1gb
$sumsizedecimal = [math]::Round($sumsize,3)
$sumitem = ($tenantinfo.TotalItems | Measure-Object -Sum).sum

"Total Mailbox data in your tenant is $sumsizedecimal GB"
"Total number of Mailbox items in your tenant is $sumitem"

Leave a comment