Data Centers optimize

PowerShell script: Get mailbox averages

Scott Lowe shows you a useful PowerShell script that you can run to get important information on Exchange mailboxes.

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

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
       $mailboxCount++
       # 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 "-------------------"
Write-Host "Average Mailbox Size:                  $mailboxAverageSize MB"
Write-Host "Average Items per Mailbox:             $mailboxAverageItemCount"

The results

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.

About

Since 1994, Scott Lowe has been providing technology solutions to a variety of organizations. After spending 10 years in multiple CIO roles, Scott is now an independent consultant, blogger, author, owner of The 1610 Group, and a Senior IT Executive w...

5 comments
IT_ShortBus
IT_ShortBus

Will this work with MS BPOS or Office 365 or is it for in house Exchanges? Thanks

fhrivers
fhrivers

I added this line at the beginning to automate the process for those of us with mulitple databases. It simply prompts you for the DB name. $maildb = read-host "Enter the mailbox database name" Then I slightly modified the second line: $listOfMailboxes = Get-MailboxDatabase $maildb | Get-Mailbox Awesome script! I'm working on setting up database size limits and this came in handy.

jadham
jadham

I had a problem running it on our Exchange 2010. Please see below: The term 'Get-MailboxDatabase' is not recognized as the name of a cmdlet, function, script file, or operable program. heck the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\mailboxstats.ps1:3 char:39 + $listOfMailboxes = Get-MailboxDatabase

techrepublic
techrepublic

i like that - but in my environment we have two mailbox clusters, and LOTS of databases - so i tweaked the script slightly - this version simply accesses all mailbox databases, and outputs the same information for each database, and then a total for all databases # Retrieve the list of mailboxes from all mailbox databases on all servers $listofmailboxdatabases = get-mailboxdatabase $totalmailboxcount = 0 $totalmailboxtotalitemcount = 0 $totalmailboxtotalsize = 0 # start a loop that will check each mailbox database foreach ($individualdatabase in $listofmailboxdatabases) { $listOfMailboxes = Get-MailboxDatabase $individualdatabase | 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 $mailboxCount++ # 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 Write-Host "Stats for mailbox database $individualdatabase" 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 "-------------------" Write-Host "Average Mailbox Size: $mailboxAverageSize MB" Write-Host "Average Items per Mailbox: $mailboxAverageItemCount" $totalmailboxcount = $totalmailboxcount + $mailboxCount $totalmailboxtotalsize = $totalmailboxtotalsize + $mailboxTotalSize $totalmailboxtotalitemcount = $totalmailboxtotalitemcount + $mailboxTotalItemCount } # Calculate the average mailbox size $totalmailboxAverageSize = $totalmailboxtotalsize / $totalmailboxcount # Calculate the average number of items per mailbox $totalmailboxAverageItemCount = $totalmailboxtotalitemcount / $totalmailboxcount Write-Host "-------------------" Write-Host "-------------------" Write-Host "Total Number of Mailboxes: $totalmailboxcount" Write-Host "Total Size of Mailboxes: $totalmailboxtotalsize MB" Write-Host "Total Items in Mailboxes: $totalmailboxtotalitemcount" Write-Host "-------------------" Write-Host "Average Mailbox Size: $totalmailboxAverageSize MB" Write-Host "Average Items per Mailbox: $totalmailboxAverageItemCount"

Neon Samurai
Neon Samurai

If it has not been covered previously, it is also worth dumping a list of user folders based on a chosen item count limit. Exchange is not really affected by the size of a mailbox but rather the number of items in a single folder (items in a table in the exchange database). For dumping a list of all user folders with more than X (eg. 5000) items sorted by username, folder item count and redirected to a text file: [command/script example removed. aperently including it makes TR's CMS display only the first and last line of my comment.] (I need to take five minutes and write this into proper shell script one day soon)