PowerShell is good stuff and can really streamline an Exchange administrator's duties. From gathering simple pieces of information to completely automating routine tasks, PowerShell should be considered one of Microsoft's crown jewels.

In this article, we'll investigate the Get-MailboxStatistics cmdlet and use it as a way to demonstrate some foundational PowerShell structures.

Get-MailboxStatistics

This cmdlet is a basic building block when it comes to gathering information about Exchange mailboxes. The cmdlet returns a ton of information and can also be used to determine the last time a mailbox was accessed and retrieve a history of mailbox moves between servers or databases.

You can't use the Get-MailboxStatistics simply by typing it at a PowerShell command prompt. You need to give it at least one of three parameters:

Database. When you specify a database, all of the mailboxes for that database are returned. If you have a lot of mailboxes, this can result in a lot of information. If your database name has spaces in it, be sure to enclose the database name in quotation marks.

Identity. Use the Identity parameter to return the results from a single mailbox.

Server. You can use the Server parameter to have the cmdlet return all of the mailboxes that are present on the specified server. Again, this could result in a whole lot of information being returned.

There are a couple of other parameters that might be useful, depending on your needs:

Archive. If you specify this parameter, you can return mailbox statistics for the archive mailbox that is associated with a mailbox. Since archived mail still uses disk space, this can be an important consideration in storage planning.

IncludeMoveHistory. Mailboxes can be easily moved between servers and databases. If you'd like to get information about the move history of the mailbox, use this switch.

IncludeMoveReport. This switch specifies whether to return a detailed move report for a completed move request.

In Figure A, you can see some of the results of running the Get-MailboxStatistics cmdlet against an existing database. Note that this is a sample database so there isn't a lot in there. That said, you can see the number of items in each mailbox, gain at-a-glance information about whether or not the mailbox size is within limits and get a look at the last time the mailbox was accessed. A note of caution: Be a bit careful about using this last accessed date as proof for too much; some backup tools access the mailbox in such a way that a regular backup will cause this date to be changed.

Figure A

A first look at the Get-MailboxStatistics cmdlet (click screenshots to enlarge)

If you're disappointed at the paltry amount of information that was returned, don't be. The view you see in Figure A is a table view of the information returned from the cmdlet. A list view is another PowerShell view that will list a whole lot more information. For sanity's sake, let's retrieve the information from just a single mailbox this time and return the results in a list view using the following command:

Get-MailboxStatistics -identity administrator | fl

Figure B shows you the results of this command.

Figure B

A list view from the Get-MailboxStatistics cmdlet

As you can see in this result, a complete look at the mailbox statistics is returned this time. Much more information, including item count, last logon and logoff time, and server name is included.

Using the individual elements

Being able to view this information is all well and good, but let's look at some PowerShell basics. How do you go about making use of the individual items returned in the list view? For example, how could you access one single element?

There are a couple of different ways, actually. First of all, you could pass to the fl filter a list of the fields you want to see (i.e., Get-MailboxStatistics -identity administrator | fl displayname, servername, storagelimitstatus will return the information shown in Figure C).

Figure C

Return just the fields that you want

Again, this is good, but still doesn't give you a way to access an individual element to use in, for example, calculations.

The simplest way to go about making the mailbox statistics is to create a variable in which to hold the returned values and then access those values using PowerShell's native capabilities.

Here's an example:

# Assign the Administrator's mailbox statistics to a variable

$mailboxinfo = Get-MailboxStatistics -identity administrator

Now, if you simply type $mailboxinfo at a PowerShell prompt.

Here are the results.

Figure D

The variable now holds the Administrator's mailbox stats.

With the information in this variable, each of the elements that you saw before is now a property of that variable and can be directly accessed and used in calculations. Try the following commands and see what you get:

$mailboxinfo.displayname

$mailboxinfo.AssociatedItemCount * 5

IF ($mailboxinfo.isarchivemailbox) {"Mailbox is an archive mailbox"} else {"Mailbox is not an archive mailbox"}

Here are the results I get running these commands.

Figure E

Using the individual elements directly

The first command simply displays the name of the account that is held in the variable. In the second, I've demonstrated that you can use one of the variable elements in mathematical calculations. For the third command, note that I've used another element of the newly created variable to make a decision about what to do next. It's a simple IF statement, but demonstrates that you can use variable elements to direct programming flow.

Summary

You've learned how Get-MailboxStatistics can be used in a few different ways and you've also been introduced to different ways to gather information and then use the individual elements for other scripting purposes.

About Scott Lowe

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...

Full Bio

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 with CampusWorks, Inc. Scott is available for consulting, writing, and speaking engagements and can be reached at slowe@email.com.