Tuesday, 3 March 2009

Combining two commands get-mailbox and get-mailboxstatistics

Did you ever needed to supply a list of mailboxes containing the e-mail address, DisplayName, Database, TotalSize of mailbox and total Items of the mailbox.
There are various ways of obtaining this information but the easiest way would be with powershell.
The information you need is supplied by two commands:
  1. Get-mailbox
  2. Get-mailboxstatistics
So how do you get the information of those two commands in a single output?

Get-mailbox Name select-Object DisplayName,PrimarySmtpAddress,Database
Get-Mailboxstatistics Name Select-Object TotalItemSize,ItemCount

I used to create arrays and then merge them together but for this relatively simple task you need at least 15 lines of code.
So I looked for other ways doing this.


To get the size of a mailbox you use the command:

Get-MailboxStatistics MBXName select TotalItemSize,ItemCount

This will produce:
TotalItemSize is displayed in Bytes which is not easy to work with so you can use the following expression to get it in another format.

Get-MailboxStatistics MBXName select {$_.TotalItemSize.value.toMB()},ItemCount

This will produce:

This is still not easy to work with. We need a different header for the mailbox size.

Get-MailboxStatistics MBXName select @{n="Size(MB)" ;E = {$_.TotalItemSize.value.toMB()}},ItemCount

This will produce:
So we can use an expression to reformat the output. So can we use another command in the same way we reformatted the output? Yes you can.

Get-Mailbox MBXName Select-Object name,primarysmtpaddress,DisplayName,Database, @{e = {Get-MailboxStatistics $_.name select totalItemsize}}

This will produce:
The columnheader of "Get-MailboxStatistics ......" is actually:

Get-MailboxStatistics $_.name PipelineBreakpointerE4078E3092DF4dd9A469F3DC0CBB505C(00000000000000000110) select totalItemsize

This is stil not easy to work with.
If you combine the statement to convert-to-MB with the get-mailboxstatistics you can get something like this:

Get-Mailbox MBXName Select-Object name,primarysmtpaddress, DisplayName,Database,@{n="Size(MB)";e = {$MBXstat = Get-MailboxStatistics $_.name; $MBXstat.totalItemsize.value.toMB()}},@{n="Items"; e = {$MBXstat = Get-MailboxStatistics $_.name ; $MBXstat.itemcount}}

This will output:
If you want to create an overview of a complete database you would use the following command:

Get-Mailbox –Database SRV1\SG01\DB01 Select-Object name,primarysmtpaddress,DisplayName,Database,@{n="Size(MB)";e = {$MBXstat = Get-MailboxStatistics $_.name; $MBXstat.totalItemsize.value.toMB()}},@{n="Items"; e = {$MBXstat = Get-MailboxStatistics $_.name ; $MBXstat.itemcount}}


Remco

9 comments:

  1. i'm in mix mode with exchange 2003 and exchange 2007..

    2 questions.

    1) can i get all exchange mailboxes size, display and email addresses ?

    2) can i just get all exchange 2007 mailboxes since it may not able to run to get on both version of exchange server?

    Jimmywang100 at yahoo

    ReplyDelete
  2. @Jimmywang100:
    If you want to retrieve E2K3 mailbox sizes you have to use WMI. This can be done with powershell:

    Get-WMIObject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer ExchangeServerName | sort-object MailboxDisplayName | ft MailboxDisplayName,Servername,StorageGroupName,StoreName,Size

    You can use the get-mailbox command for mailboxes on E2K3 but alot of time these will display warning messages.

    The best way for E2K3 is to use WMI.

    ReplyDelete
  3. Excellent Post
    This helped me indirectly to build my own command
    where I wanted to find the mailboxes with string " Support" and then their mailbox stats

    Just in case if some one require that command
    its here


    _______________________________
    Get-Mailbox -filter { DisplayName -like '*support*'} -ResultSize Unlimited -IgnoreDefaultScope |Get-MailboxStatistics | select Displayname,ItemCount,@{n="Size(MB)" ;E = {$_.TotalItemSize.value.toMB()}}
    _______________________________

    NOTE- Remove spaces before using the command.

    Thanks again

    Badhwar Nitin

    ReplyDelete
  4. Hi Guys try this. you can also get the department attribute with the details


    Get-Mailbox |Select name,alias, @{n="Size(MB)";e = {$MBXstat = Get-MailboxStatistics $_.name; $MBXstat.totalItemsize.value.toMB()}},@{n="Department"; e = {$MBXstat = Get-user $_.name ; $MBXstat.Department}}

    ReplyDelete
  5. I've been browsing online more than 3 hours today, yet I never found any interesting article like yours. It's pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the web will be a lot more useful than ever before.
    My webpage - exchange server 2003 mailbox Recovery

    ReplyDelete
  6. This is really useful, however I encounter problems when trying to export the output to *.csv

    This works for one user:
    [PS] C:\>Get-Mailbox MBXname |select-object name,displayname,identity,alias,database,@{n="Size(MB)";e = {$MBXstat = Get-MailboxStatistics $_.name; $MBXstat.totalItemsize.value.toMB()}},@{n="Items"; e = {$MBXstat = Get-MailboxStatistics $_.name ; $MBXstat.itemcount}} | export-csv C:\user.csv

    However, this fails to export the size & item count information:
    [PS] C:\>Get-Mailbox -Database DBname |select-object name,displayname,identity,alias,database,@{n="Size(MB)";e = {$MBXstat = Get-MailboxStatistics $_.name; $MBXstat.totalItemsize.value.toMB()}},@{n="Items"; e = {$MBXstat = Get-MailboxStatistics $_.name ; $MBXstat.itemcount}} | export-csv C:\users.csv

    ReplyDelete
    Replies
    1. mee too~~~ but i find complete way...~~!!!
      add sort-object...
      exam) Get-Mailbox -Database "writeyourDatabase" | sort-object | select-object name,primarysmtpaddress,displayname,database,ServerName,@{n="TotalItemSizeMB";e = {$MBXstat = Get-Mailboxstatistics $_.name; $MBXstat.totalItemsize.value.toMB()}},@{n="ItemsCount"; e = {$MBXstat = Get-Mailboxstatistics $_.name; $MBXstat.itemcount}}, @{n="StorageLimitStatus"; e = {$MBXstat = Get-Mailboxstatistics $_.name; $MBXstat.StorageLimitStatus}}


      find url :
      http://social.technet.microsoft.com/Forums/exchange/en-US/82d5d38a-0c13-4c16-b1b0-9999c00d354d/mailbox-size-reports-and-other-stuff-exchange-powershell-2010?forum=exchange2010

      Delete
  7. This saved my life... Thank you so much!!! I wasn't finding a way to join this two commands... Many thanks!!!

    ReplyDelete
  8. Thanks, this is just what I was looking for! One issue I did find was that if you have more than one mailbox with the same name (perfectly possible with multiple OUs, storage groups etc), then those duplicates (both copies) get returned with size and item count black. Changing the instances of $_.name to $_.identity fixed that, since obviously "name" isn't required to be unique, but "identity" always is.

    Also, if you have multiple mailbox servers, you can pipe "Get-MailboxDatabase" to your existing Get-Mailbox command, but without the -Database parameter, and it will query all mailboxes on all mail servers within the one query.

    ReplyDelete