I love PowerShell. It does a whole lot and it’s just getting better all the time. As you probably know, Microsoft has deeply embedded PowerShell functionality throughout all of the company’s new products. This scripting capability can be quickly and easily leveraged to capture pertinent information from various sources, including Exchange databases.
Exchange administrators often need to capture statistics from the various mailbox databases under their careful watch. For many, it’s important to gain an understanding for how the Exchange database is growing so that steps can be taken to plan for future storage.
In the script below, all of the mailbox information is gathered in aggregate and the user is presented with a display that shows the total number of mailboxes in the database, the total size of the mailboxes, and the total number of items in each mailbox. The script also displays the average mailbox size and the average number of items per mailbox.
The script below accomplishes the goals outlined above. I have commented every section so that you know exactly what’s happening every step of the way.
# Retrieve the list of mailboxes from the specified mailbox database
$listOfMailboxes = Get-MailboxDatabase "Mailbox Database 1081629644" | Get-Mailbox
# Initialize the counter variables that we'll use
$mailboxCount = 0
$mailboxTotalItemCount = 0
$mailboxTotalSize = 0
$mailboxAverageSize = 0
$mailboxAverageItemCount = 0
# Start a loop that will count stats from individual mailboxes
foreach ($individualMailbox in $listOfMailboxes)
# increment the mailbox count by 1
# Get the name of the current mailbox so that we can...
$individualMailboxName = $individualMailbox.Identity.DistinguishedName
#... quickly and easily get stats from that mailbox
$individualMailboxStats = Get-MailboxStatistics -Identity $individualMailbox
# Get the size of the mailbox in MB and save it in a variable
$individualMailboxSize = $individualMailboxStats.TotalItemSize.value.toMB()
# Get the number of items in the mailbox and save it in a variable
$individualMailboxItemCount = $individualMailboxStats.ItemCount
# Add the size of this mailbox to a running total
$mailboxTotalSize = $mailboxTotalSize + $individualMailboxSize
# Add the number of items in this mailbox to a running total
$mailboxTotalItemCount = $mailboxTotalItemCount + $individualMailboxItemCount
# Calculate the average mailbox size
$mailboxAverageSize = $mailboxTotalSize / $mailboxCount
# Calculate the average number of items per mailbox
$mailboxAverageItemCount = $mailboxTotalItemCount / $mailboxCount
# Display the results to the user
Write-Host "Total Number of Mailboxes in database: $mailboxCount"
Write-Host "Total Size of Mailboxes: $mailboxTotalSize MB"
Write-Host "Total Items in Mailboxes: $mailboxTotalItemCount"
Write-Host "Average Mailbox Size: $mailboxAverageSize MB"
Write-Host "Average Items per Mailbox: $mailboxAverageItemCount"
Once you’ve created the script, run it. You will get results much like the ones shown below:
Total Number of Mailboxes in database: 54
Total Size of Mailboxes: 270 MB
Total Items in Mailboxes: 55
Average Mailbox Size: 5 MB
Average Items per Mailbox: 1.01851851851852
As you can see, this script was being run in a test environment so there’s not a lot of exciting stuff to see. However, I’d be willing to bet that, if you run the script in your own environment, the results would be a whole lot more interesting.