Improving performance with Get-MailboxFolderStatistics

In a previous post I mentioned some performance improvements I made in an Exchange script that used Get-MailboxFolderStatistics. A portion of this script selected the size (in megabytes) of the top 5 folders in each users mailbox. I made a subtle change to the code that saved about 35 minutes on the execution time of the script.

For purposes of the following examples, I measured the execution time of this block of code using my Exchange mailbox. My mailbox has about 250 folders and is about 1.4GB in size.

Previous version of code takes 0.2329 seconds to complete.
[cc lang=”powershell”]
measure-command {
# Previous
$stats = Get-MailboxFolderStatistics $mbx.identity
$folders = $stats | Select-Object folderpath,@{name=”Size(MB)”;Expression={$_.FolderAndSubfolderSize.toMB()}}
$folders = $folders | Where-Object {$_.”Size(MB)” -ne 0} | Sort-Object -Property “Size(MB)” -Descending | Select-Object -First 5
$folders
}
[/cc]

Updated version of code takes 0.1418 seconds to complete.
[cc lang=”powershell”]
measure-command {
# Updated
$stats = Get-MailboxFolderStatistics $mbx.identity
$folders = $stats | Where-Object {$_.FolderAndSubfolderSize -gt 1048576} | Sort-Object -Property “FolderAndSubfolderSize” -Descending | Select-Object -First 5
$folders = $folders | Select-Object folderpath,@{name=”Size(MB)”;Expression={$_.FolderAndSubfolderSize.toMB()}}
$folders
}
[/cc]

On my account, this is a savings of only 0.0911 seconds. That doesn’t sound like enough savings to implement, does it? Actually, if you take my mailbox example and expand it by 40,000 users it would be a savings of about 1 hour. My mailbox is likely larger than the average user, which explains why the savings is closer to 35 minutes instead of 60.

Can you notice the change in the code? It is subtle…nearly the same code/commands are being used — only the order was changed slightly.

In the previous version of the script, all of the folders were selected into an object and the ToMB method was called on the FolderAndSubfolderSize property. The next line of code selects only the first five non-zero entries.

In the updated version of the code, only the first five non-zero entries were added to the object. The next line of code then called the ToMB method on only the first five folders. In my example, this is a savings of about 245 calls on the ToMB method.

This entry was posted in Messaging, Scripting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.