Retrieve information about Mailboxes and Mailbox Databases with PowerShell

Recently, I was faced with a situation in which I wanted to remove a Mailbox Database that was running in production. The problem was that I wasn’t sure which mailboxes resided on the database. Moreover, a quick and efficient way of finding this information wasn’t apparent in the Exchange Management Console and I wasn’t about to sort through my entire Exchange organization to find this subset of users. The answer? The Exchange Management Shell.

[ps] Get-Mailbox -Identity * | Get-MailboxStatistics | Where {$_.Database -Match ""} | Format-Table DisplayName,Database,ServerName [/ps]

Dissecting the code
Get-Mailbox –Identity *: The Get-Mailbox Cmdlet retrevies mailboxes within the Exchange environment, –Identity *
instructs the Cmdlet to retrieve information for every mailbox in the Exchange environment.
(A note about result throttling:
By default, the Get-Mailbox Cmdlet will return up to 1,000 results. This is due to the fact that if it were to return every mailbox in your environment the potential for extremely large amounts of data being returned comes into play. If you know more than 1,000 users will be in your result set simply include –resultsize or –resultsize unlimited. For example, Get-Mailbox –Identity * -resultsize 2000.)
Get-Mailboxstatistics : Get-Mailboxstatistics provides a wealth of information about mailboxes in the Exchange environment.
Where {$_.Database -Match “”}: This Where Clause is really heart and soul of the script. In a nutshell, it allows us to filter the objects that are returned from the Get-Mailboxstatistics Cmdlet. In this case we’re filtering on the Database property, but we can filter on
quite a few different properties (please see the table below for a complete list of these properties).
Name
Type
AssociatedItemCount
Property
DatabaseName
Property
DisconnectDate
Property
DisplayName
Property
IsArchiveMailbox
Property
IsValid
Property
LastLoggedOnUserAccount
Property
LastLogonTime
Property
MailboxGuid
Property
MapiIdentity
Property
ObjectClass
Property
ServerName
Property
Database
Property
DeletedItemCount
Property
DisconnectReason
Property
Identity
Property
IsQuarantined
Property
ItemCount
Property
LastLogoffTime
Property
LegacyDN
Property
MailboxTableIdentifier
Property
MoveHistory
Property
OriginatingServer
Property
StorageLimitStatus
Property
TotalItemSize
Property
Format-Table DisplayName,Database,ServerName: Here we’re simply asking PowerShell to format the output of objects in the pipeline into a table for easier viewing. Lastly we’re performing an implicit Select-Object and requesting just the DisplayName, Database and ServerName be displayed as part of the final output.
The Script in action
Now that we’ve dissected the code, let’s look at what the output of the script looks like:

Interactive Script Output

From Script to Tool
Our script is returning the information we want, but it’s not exactly user friendly. By this I mean that you must open the script in an editor and manually change the Database you’re querying for any time you want to look for users residing on another Mailbox Database. To fix this we simply define a variable that accepts user input.

[ps] $Database = Read-Host "Please Enter the name of a Mailbox Database to query"

Get-Mailbox -Identity * | Get-MailboxStatistics | Where {$_.Database -Match $Database} | Format-Table DisplayName,Database,ServerName [/ps]

In the next iteration of the script I’ll allow a user to define what which property they would like to query for in the same way they specify the Mailbox Database.

User prompt for input
That’s it! You can use this script as a flexible inventory tool to determine which mailboxes are on a target Mailbox Database (or most anything else) without having to rely onthe Exchange Management Console. In a future article we’ll look at extending this code into an end-to-end Mailbox Database Move solution.
//J
(Note 1: You may be wondering why I’ve included the Get-MailboxStatistics Cmdlet since the Get-Mailbox Cmdlet contains the Database and DisplayName properties. I’ve included it because it contains some properties Get-Mailbox doesn’t and allows the script to be used as more of an inventory tool.)

(Note 2: The beauty of PowerShell is that you can accomplish the same goal multiple ways sometimes. This is one of those situations. You could simply execute

[ps]Get-Mailbox –Database <"Database Name">[/ps]

If you wanted nothing more than the mailboxes on that Mailbox Database. While I started out wanting only this information, I later wanted some additional information and wanted to provide my readers with something a little more interesting.)

Leave a Reply

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