Friday, July 31, 2015

Reporting Mailbox Usage Statistics

Hi folks,

Just wanted to share with you a script I have recently used to extract information about the mailboxes, their size and also dates of the newest and the oldest item. It can be extremely useful for planning activities when you're trying to identify the size of the mailboxes in your environment to plan potential migration traffic and also storage for Exchange or Office 365 migration activities.

I want to thank authors of this thread for giving me this hint.

As the result I have got something as below:


#This script can be run against a single mailbox, group of mailboxes or all mailboxes. All you need is to replace value for $mbx variable
#if you run it against a single mailbox set $mbx to something like $mbxs = Get-Mailbox -Identity john.smith
#if you run it against mailboxes in a certain region filter it by domain, like Get-Mailbox -Filter {EmailAddresses -Like "*contoso.com*"}
#If you run it against all mailboxse just use $mbxs = Get-Mailbox -ResultSizeUnlimited


$mbxs = Get-Mailbox -Filter {EmailAddresses -Like "*contoso.com*"} -ResultSize Unlimited
$mbxs | %{
    $newest = $oldest = $null
    $mb = $_
    $mb | Get-MailboxFolderStatistics -IncludeOldestAndNewestItems | %{
        if($_.NewestItemReceivedDate -and (!$newest -or $newest -lt $_.NewestItemReceivedDate.tolocaltime())){
            $newest = $_.NewestItemReceivedDate.tolocaltime()}
        if($_.OldestItemReceivedDate -and (!$oldest -or $oldest -gt $_.OldestItemReceivedDate.tolocaltime())){
                $oldest = $_.OldestItemReceivedDate.tolocaltime()}
    }
    $stat = $mb | Get-MailboxStatistics
    New-Object -TypeName psobject -Property @{
        DisplayName = $mb.displayname
        SMTPAddress = $mb.PrimarySMTPAddress.tostring()
        TotalItemSize = $stat.TotalItemSize
        TotalItems = $stat.itemcount
        RetentionPolicy = $mb.retentionpolicy
        NewestItem = $newest
        OldestItem = $oldest    
    }
} | Select-Object -Property DisplayName, SMTPAddress, TotalItems, TotalItemSize, RetentionPolicy, NewestItem, OldestItem |
    Export-Csv "D:\Scripts\MbxStats.csv" -NoTypeInformation

It will be output into the nice and readable Excel file like this:



Enjoy