Getting size of all Exchange Mailbox folders

There’s quite a few reasons why you would need to get the size of mailbox folders. If someone has run out of space or you just need to report on folders for some reason.

Import-Module ExchangeOnlineManagement

# $UserCredential = Get-Credential
# Connect-ExchangeOnline -UserPrincipalName $UserCredential.UserName -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

Leave a Reply

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