Search code examples
powershellexchange-server

Get-mailbox statistics and quota details


I have the below script to get info from all mailboxes with the quota details, but in the report.csv file I don't have free space details like you can see in the example below:

$results=ForEach($mb in $mailboxes){
    $stats=get-mailboxstatistics $mb
    $props=@{
        alias=$mb.alias
        DisplayName=$mb.displayname
        #StorageLimitStatus=$stats.StorageLimitStatus
        TotalItemSize=$stats.totalitemsize
        #DatabaseName=$stats.databasename
        ProhibitSendQuota=$mb.ProhibitSendQuota
        ProhibitsendReceiveQuota=$mb.ProhibitsendReceiveQuota
        IssueWarningQuota=$mb.IssueWarningQuota
    }
    New-Object PsObject -Property $props
}
$results | Sort-Object TotalItemSize -descending | export-csv c:\script\report.csv -NoTypeInformation -Encoding UTF8

Output :

Alias , Display Name ,TotalItemSize, IssueWarningQuota ,ProhibitsendReceiveQuota , ProhibitSendQuota

User01, User01 ,  46.51 GB (49,935,441,080 bytes) , unlimited , unlimited , unlimited
User02, User02 ,  4.887 GB (5,247,750,394 bytes)   5.86 GB (6,292,127,744 bytes) , 5.95 GB (6,388,764,672 bytes) , 5.91 GB (6,345,815,040 bytes)

My desired output is :

Alias , Display Name ,TotalItemSize, IssueWarningQuota ,ProhibitsendReceiveQuota , ProhibitSendQuota  , Free Space

User01, User01 ,  46.51 GB (49,935,441,080 bytes) , unlimited , unlimited , unlimited  , unlimited
User02, User02 ,  4.887 GB (5,247,750,394 bytes)   5.86 GB (6,292,127,744 bytes) , 5.95 GB (6,388,764,672 bytes) , 5.91 GB (6,345,815,040 bytes) , 1.023 GB

Free space calculation formula is : ( Free Space = ProhibitSendQuota - TotalItemSize)

If ProhibitSendQuota is unlimited then free space is unlimited.

If ProhibitSendQuota is custom size then free space is Free Space = ProhibitSendQuota - TotalItemSize.

I also need to get the user account name with the Free Space.

How can I achieve these things?


Solution

  • You could do something like the following:

    $results = ForEach($mb in $mailboxes){
        $stats=get-mailboxstatistics $mb
        if ($mb.ProhibitSendQuota -eq 'Unlimited') {
            $freespace = 'Unlimited'
        } 
        else {
            $totalBytes = [double]($stats.totalitemsize -replace '.*?\((.*?) bytes.*','$1')
            $prohibitBytes = [double]($mb.ProhibitSendQuota -replace '.*?\((.*?) bytes.*','$1')
            $freespace = [Math]::Round(($prohibitBytes - $totalbytes)/1GB,2)
        }
        $props=@{
            alias=$mb.alias
            DisplayName=$mb.displayname
            #StorageLimitStatus=$stats.StorageLimitStatus
            TotalItemSize=$stats.totalitemsize
            #DatabaseName=$stats.databasename
            ProhibitSendQuota=$mb.ProhibitSendQuota
            ProhibitsendReceiveQuota=$mb.ProhibitsendReceiveQuota
            IssueWarningQuota=$mb.IssueWarningQuota
            FreeSpace=$freespace
        }
        [pscustomobject]$props
    }
    
    $results | Sort-Object TotalItemSize -descending | export-csv c:\script\report.csv -NoTypeInformation -Encoding UTF8
    

    Explanation:

    if and else statements were added to handle conditions of Unlimited quota versus a numerical value. $totalBytes and $prohibitBytes select the bytes value of the ## GB (#,###,###,### Bytes) format and converts it to type [double]. Once those values are number value types, we can perform subtraction. The /1GB converts the result to GB. Round() method rounds to the nearest hundredths because of the ,2 argument value.


    It may be worth looking into a more elegant solution using the ByteQuantifiedSize structure. I just found this way to be easier to implement.