Exchange 2013 Powershell Script: Get DAG-wide Mailbox Statistics

While preparing for our On-prem -> O365 migration of a subset of our users, we ran across the need to gather mailbox statistics so that we could create the migration batches based on metrics.

It’s pretty simple for us to do so as the subset of users were housed on one of our DAGs, so all we needed to do was query all of the servers for all of the mailboxes, get the stats, package them up, and dump it to a CSV in a meaningful format so that we could use it both for the scoring and analysis scripts.

This script needs little preparation to run in your environment – you could actually run it against all dags with a bit of work, but I was focused on just 1 for my migration.

Modify the “$DagName” variable with your exact DAG name, save it, and run it. It will take a while to run since it’s enumerating all mailboxes and gathering data, and for larger organizations it will be a bit of a memory hog as it’s creating an array of objects in memory for its duration before it dumps them out to CSV.

$DagName = "DAG01"
$DateTime = Get-Date
$FormattedDateTime = $DateTime.Year.ToString() + "." + $DateTime.Month.ToString() + "." + $DateTime.Day.ToString() + "." + $DateTime.Hour.ToString() + "." + $DateTime.Minute.ToString() + "." + $DateTime.Second
$OutputFile = ".\$($DagName)MailboxStats-" + $FormattedDateTime + ".csv"

$Results = @()
$DAG = Get-DatabaseAvailabilityGroup $DagName -Status

foreach($Server in $DAG.StartedMailboxServers)
    $Mailboxes = get-mailbox -Server $Server -ResultSize unlimited

    foreach($Mailbox in $Mailboxes)
        $Statistics = get-mailboxstatistics $Mailbox
        $output = New-Object PSObject
        # Add members to the PSObject
        $output | Add-Member -type NoteProperty -name Name -value $($
        $output | Add-Member -type NoteProperty -name SamAccountName -value $($Mailbox.samaccountname)
        $output | Add-Member -type NoteProperty -name UserPrincipalName -value $($Mailbox.UserPrincipalName)
        $output | Add-Member -type NoteProperty -name DN -value $($Mailbox.distinguishedname)
        $output | Add-Member -type NoteProperty -name OrganizationalUnit -value $($Mailbox.OrganizationalUnit)
        $output | Add-Member -type NoteProperty -name CustomAttribute1 -value $($Mailbox.CustomAttribute1)
        $output | Add-Member -type NoteProperty -name ItemCount -value $($Statistics.ItemCount)
        $output | Add-Member -type NoteProperty -name DeletedItemCount -value $($Statistics.DeletedItemCount)
        $output | Add-Member -type NoteProperty -name AssociatedItemCount -value $($Statistics.AssociatedItemCount)
        $output | Add-Member -type NoteProperty -name TotalItemSize -value $($Statistics.TotalItemSize -replace "(.*\()|,| [a-z]*\)", "")
        $output | Add-Member -type NoteProperty -name TotalDeletedItemSize -value $($Statistics.TotalDeletedItemSize -replace "(.*\()|,| [a-z]*\)", "")
        # Add the output object to the master Results object, which is returned on script completion
        $Results += $output

$Results | Export-Csv -Path $OutputFile -NoTypeInformation

You will need to be in an Exchange 2013 prompt to run this, and have the requisite permissions in the Exchange org to access the stats.

This entry was posted in Exchange 2013, Management, Powershell and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s