PowerShell: list mailbox database statistics (quota, mailbox count and size) – version 2
This script now includes reading the quota settings per mailbox database. There is no need to specify the input, as it automatically iterates through all mailbox databases in the organization (as long as your user account has sufficient permissions to see them). Then it starts building up a table object and filling rows with the results. This works pretty well, since you can use the table for sorting, displaying and usage in other scripts.
After you execute the below script, you will get a $table object containing all the data. To filter and sort this, use for example:
$table | where-object {$_.MailboxCount -gt 1} | sort “MailboxCount” | ft -autosize
The above statements filters and keeps all rows where the MailboxCount column is greather than 1, then sorts on that column and formats the table as output.
Another example is:
$table | where-object {$_.IssueWarningQuotaMB -gt 300 -and $_.IssueWarningQuotaMB -lt 1000} | sort “MailboxCount” | ft -autosize
The script to generate $table:
$table = New-Object system.Data.DataTable “$myTable”
$col1 = New-Object system.Data.DataColumn Identity,([string])
$col2 = New-Object system.Data.DataColumn MailboxCount,([decimal])
$col3 = New-Object system.Data.DataColumn SizeMB,([decimal])
$col4 = New-Object system.Data.DataColumn ProhibitSendQuotaMB,([decimal])
$col5 = New-Object system.Data.DataColumn ProhibitSendReceiveQuotaMB,([decimal])
$col6 = New-Object system.Data.DataColumn IssueWarningQuotaMB,([decimal])
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
$dbs = Get-MailboxDatabase
foreach ($db in $dbs)
{
$dbstats = Get-MailboxStatistics -database $db
$totaldbsize = 0
$totalmbcount = 0
foreach ($dbstat in $dbstats)
{
$totalmbcount = $totalmbcount + 1
$totaldbsize = $totaldbsize + $dbstat.TotalItemSize.Value.ToMB()
}
$row = $table.NewRow()
$row.Identity = $db.Identity
$row.MailboxCount = $totalmbcount
$row.SizeMB = $totaldbsize
$row.ProhibitSendQuotaMB = $db.ProhibitSendQuota.Value.ToMB()
$row.ProhibitSendReceiveQuotaMB = $db.ProhibitSendReceiveQuota.Value.ToMB()
$row.IssueWarningQuotaMB = $db.IssueWarningQuota.Value.ToMB()
$table.Rows.Add($row)
}
$table | sort “identity” | ft -AutoSize