Getting the Size of All Exchange Mailboxes

When users hit storage limits or you need a capacity report, it’s useful to pull mailbox sizes across the entire organisation in one go. The script below uses the Exchange Online Management module to collect primary and archive mailbox sizes for every mailbox and export the results to CSV.

The script

Import-Module ExchangeOnlineManagement

# Connect-ExchangeOnline -UserPrincipalName admin@domain.com -ShowProgress $true

$mailboxes = Get-Mailbox -ResultSize Unlimited
$mailboxInfoList = @()

foreach ($mailbox in $mailboxes) {
    $mailboxStats = Get-MailboxStatistics -Identity $mailbox.PrimarySmtpAddress
    $archiveStats = if ($mailbox.ArchiveStatus -eq 'Active') {
        Get-MailboxStatistics -Archive -Identity $mailbox.PrimarySmtpAddress
    } else {
        $null
    }

    $mailboxInfo = [PSCustomObject]@{
        DisplayName      = $mailbox.DisplayName
        PrimarySMTP      = $mailbox.PrimarySmtpAddress
        'MailboxSize(MB)'  = [math]::Round(($mailboxStats.TotalItemSize.ToString().Split('(')[1].Split(' ')[0].Replace(',','') / 1MB), 0)
        ArchiveEnabled   = if ($mailbox.ArchiveStatus -eq 'Active') { 'Yes' } else { 'No' }
        'ArchiveSize(MB)'  = if ($archiveStats) { [math]::Round(($archiveStats.TotalItemSize.ToString().Split('(')[1].Split(' ')[0].Replace(',','') / 1MB), 0) } else { 'No Archive' }
    }

    $mailboxInfoList += $mailboxInfo
}

$mailboxInfoList | Format-List
$mailboxInfoList | Export-Csv -Path 'MailboxInfo.csv' -NoTypeInformation

Disconnect-ExchangeOnline -Confirm:$false

What it does

Prerequisites

Install the Exchange Online Management module if you haven’t already:

Install-Module -Name ExchangeOnlineManagement -Scope CurrentUser

Then connect before running the script:

Connect-ExchangeOnline -UserPrincipalName admin@domain.com

You’ll need an account with at least the Mail Recipients or View-Only Recipients role to run Get-Mailbox and Get-MailboxStatistics across all users.