Below is a Exchange PowerShell script that will perform a mailbox count on each database and email the results. The script is performed with the get-mailboxstatistics for each mailbox on that database as the results are much faster than the get-mailbox command.
# Create an empty HashTable to store database name and count.
$MailboxCount = @{}
# Collect Databases
$databases = Get-mailboxDatabase | Where Name -like “2013DB*” | Sort name
#Loop through each
ForEach ($database in $databases){
$MBs = Get-mailboxstatistics -database $database
$MailboxCount.Add($Database,$MBs.count)
}
#Format the Results
$MailboxCountOrdered = $MailboxCount.GetEnumerator() | Sort-Object Name | Out-String
$orderedMailboxCount = $MailboxCount.GetEnumerator() | Sort-object Value | Out-String
#Send an email:
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = “mail.server.com”
$mailmessage.from = (“MailboxCount@domain.com”)
$mailmessage.To.add(“email.address”)
$mailmessage.Subject = “Mailbox Count”
$mailmessage.Body = “Mailbox Count
The following list shows the Mailbox Count for the Databases in Ex2013. The 2 lists below are the same; one is in order of database name and the other is in order of mailbox count.
Database Order:
$MailboxCountOrdered
Count Order:
$OrderedMailboxCount
“
$smtpclient.Send($mailmessage)