PowerShell: Create a CSV Report of Mailbox Sizes

The following is a simple script to generate a mailbox size report CSV for a set of email addresses.  The full function and explanation follows.

The function takes a few parameters;  a list of email addresses to process (an array), the filename to write the CSV data to and an optional switch specifying if you want to exclude the dumpster data in the size report (defaults to $True)

function Export-MailboxSizeReport
{
    param
    (
        [String[]]$AddressList,
        [String]$FileName,
        [switch]$IncludeDumpster=$False
        
    )
    If (!(Test-Path -LiteralPath $FileName -IsValid))
    {
        Write-Verbose "$Filename is an invalid path."
        Return
    }
    $SizeReportArray=@() 
    $AddressList | For-Each{
        $MailboxStatistics=Get-MailboxStatistics $_
        $Size=$MailboxStatistics.TotalItemSize
        if($IncludeDumpster)
        {
            $Size+=$MailboxStatistics.TotalDeletedItemSize
        }
        $SizeReportObject=New-Object -TypeName psobject -Property @{EmailAddress=$_;Size=$Size}
        $SizeReportArray+=$SizeReportObject             
        Write-Verbose"$_ Size : $Size"
    }
    $SizeReportArray | Export-CSV -Path $FileName -NoTypeInformation 
}

The script starts by checking the $FileName is correct and creating an empty array ($SizeReportArray).  The script will add custom report objects for each email address to that array and it will be exported at the end.

    If (!(Test-Path -LiteralPath $FileName -IsValid))
    {
        Write-Verbose "$Filename is an invalid path."
        Return
    }
    $SizeReportArray=@()

After that, it enumerates all the email addresses in $AddressList and gets the TotalItemSize.  If $IncludeDumpster is specified as a switch then the total dumpster size is added as well.

    $AddressList | For-Each{
        $MailboxStatistics=Get-MailboxStatistics $_
        $Size=$MailboxStatistics.TotalItemSize
        if($IncludeDumpster)
        {
            $Size+=$MailboxStatistics.TotalDeletedItemSize
        }

Last, the script builds a custom PSObject with the email address and the size.  These are added to $SizeReportArray which is then written to the CSV file.

        $SizeReportObject=New-Object -TypeName psobject -Property @{EmailAddress=$_;Size=$Size}
        $SizeReportArray+=$SizeReportObject             
        Write-Verbose"$_ Size : $Size"
    }
    $SizeReportArray | Export-CSV -Path $FileName -NoTypeInformation

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s