Getting mailbox data and stats per database

Way back in the day – – I showed how to list mailboxes by the database in which they were stored.  I had a comment left asking if its possible to list only a specific mailbox and to give the mailbox size as well.

To recap:

Get-Mailbox will return the list of mailboxes

This will quickly show the number of mailboxes per database

Get-Mailbox | group Database –NoElement

This shows the mailboxes in a particular database

Get-Mailbox -Database MDB1

To get the size you use Get-MailboxStatistics

So to put this together:

function get-mbxBYdb {
param (

Get-Mailbox -Database $database |
foreach {
$stat = $_ | Get-MailboxStatistics -WarningAction SilentlyContinue
New-Object -TypeName PSObject -Property @{
   Name = $($_.DisplayName)
   Address = $($_.PrimarySmtpAddress)
   Database = $database
   Items = $stat.ItemCount
   ‘Size(KB)’ = $stat.TotalItemSize.Value.ToKB()



The database name is a mandatory parameter.  Get the mailboxes in the database and foreach get the mailbox statistics.  You can then create an output object that combines the data from the mailbox object and the statistics object.  Examine each type of object individually to determine the exact set of properties you need.

One trick with the size of items is that you can convert to specific size units as shown (MB, GB, TB and bytes are also available)

This entry was posted in Exchange, PowerShell V2. Bookmark the permalink.

2 Responses to Getting mailbox data and stats per database

  1. Wayne says:

    I put the function into memory but running it complains about pipelines:
    cmdlet get-mbxBYdb at command pipeline position 1
    Supply values for the following parameters:
    database: SL1-PHPDB001
    The pipeline was not run because a pipeline is already running. Pipelines cannot be run concurrently.
    + CategoryInfo : OperationStopped: (Microsoft.Power…tHelperRunspace:ExecutionCmdletHelperRunspace) [],
    + FullyQualifiedErrorId : RemotePipelineExecutionFailed

    Get-MailboxStatistics : The session WinRM1, 6a000091-d006-463c-a16b-5eb9960e9371, ph**** is not available to run commands. The session availability is Busy.
    At line:10 char:2
    + $stat = $_ | Get-MailboxStatistics -WarningAction SilentlyContinue
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: ([PSSession]WinRM1:PSSession) [Invoke-Command], InvalidRunspaceStateEx
    + FullyQualifiedErrorId : InvokeCommandCommandInvalidSessionAvailability,Microsoft.PowerShell.Commands.InvokeCommandCommand

    foreach : You cannot call a method on a null-valued expression.
    At line:9 char:1
    + foreach {
    + ~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [ForEach-Object], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull,Microsoft.PowerShell.Commands.ForEachObjectCommand

    Excahnge 2010 on the server. Powershell 5.1 on my Win10 client.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s