Sunday, July 10, 2016

Getting an Integer from Get-MailboxStatistics

I was doing some analysis of an existing Exchange 2010 organization for a migration project and was wanting to calculate the average mailbox size. So, I used Get-MailboxStatistics to gather the information and export it to a csv file. Unfortunately, Get-MailboxStatistics gives you the mailbox size in this format:
2.169 GB (2,329,318,152 bytes)
When you're trying to use Excel to get an average, this isn't going to work. Instead you need to get that value as an integer. To do this, you need to create your own property for the object with an integer value.

The TotalItemSize property from Get-MailboxStatistics is more complex than it appears. When you use Get-Member to look at the TotalItemSize property returned by Get-MailboxStatistics, it actually contains an IsUnlimited property and a Value property. The Value property has the size of the mailbox.

If you do a Get-Member on the Value property, you can see that there are methods to convert the value to an integer. For example:
(Get-MailboxStatistics Bob).TotalItemSize.Value.ToMB()
To create a new property with the correct value as an integer, you use the Add-Member cmdlet. A complete 3 command example that gets all mailboxes and exports the size to csv is below:
$mbx = Get-Mailbox -Resultsize Unlimited | Get-MailboxStatistics
$mbx | Add-Member -MemberType ScriptProperty -Name MbxSizeInMB -Value {$this.TotalItemSize.Value.ToMB()}
$mbx | Select-Object DisplayName,MbxSizeInMB | Export-Csv MailboxSize.csv
Line 1 gets the mailboxes and their statistics. Line 2 adds the property. Line 3 exports the mailbox name and size to a csv file.

No comments:

Post a Comment