제2의 비엔지니어 인생관을 꿈꾸며

Posted
Filed under Study

Here’s a script I put together for Exchange 2010 to grab the mailbox size for all the users in my Exchange mailbox database, formatted, and saved to a text file.

The big ugly script. Granted, this could be larger and uglier depending upon how many mailbox statistics fields you required. For my purposes, this was all that was needed.

Get-MailboxStatistics -database ‘Exchange Mailbox Database’ | where {$_.ObjectClass -eq “Mailbox”} | Sort-Object TotalItemSize -Descending | ft @{label=”User”;expression={$_.DisplayName}},@{label=”Total Size (MB)”;expression={$_.TotalItemSize.Value.ToMB()}}  -auto >> “c:\mailbox_size.txt”

As lovely as that is, let’s break it down so you have a better idea what exactly is happening.

Get-MailboxStatistics

This cmdlet is a handy way to show the stats for specific mailboxes (as the command suggests).

Get-MailboxStatistics -database ‘Exchange Mailbox Database’

This uses the get-mailboxstatistics cmdlet and specifies that we want to grab the data from the ‘Exchange Mailbox Database’. Obviously this will change depending on what your mailbox is called. When in doubt you can run get-mailboxdatabase to find out what your databases are labeled.

| where {$_.ObjectClass -eq “Mailbox”}

Next, we pipe this data to a conditional. In this case we want to grab just the mailboxes.

| Sort-Object TotalItemSize -Descending | ft @{label=”User”;expression={$_.DisplayName}},@{label=”Total Size (MB)”;expression={$_.TotalItemSize.Value.ToMB()}}

This one is a bit hairy. Basically we are then piping the mailbox data to get sorted. Specifically we want to sort in a descending order by mailbox size then name. You may notice this bit of code: $_.TotalItemSize.Value.ToMB() That is converting the mailbox size (which is stored as bytes) to MB. We also add some handy labels to the top of the display so we know what it is exactly we are looking at when we happen to stumble upon this file a year down the road or have to send it of to someone else to decipher. Labels. Know them, use them, love them.

-auto

This auto-formats the display. Try running the script without it, you’ll see what I mean.

>> “c:\mailbox_size.txt”

And lastly, we throw the output into a text file. The ‘>>’ creates a new text file if one didn’t already exist. If it does exist, it will append the new output to the end of the current file. Likewise, if you just want to create a new file each time (no append) then you would want to use ‘>’ instead.

You can find a good description of the get-mailboxstatistics cmdlet over at this TechNet page. There’s also a handy guide on this at Petri as well.

2013/03/07 13:19 2013/03/07 13:19