Instead of reinventing the wheel I am going to use Devin blog to reference how to do this task.

http://blogs.3sharp.com/deving/archive/2007/01/08/2738.aspx

Using PowerShell to sort mailboxes by size
This weekend I was playing with the Exchange Management Shell while performing some troubleshooting on our Exchange 2007 system and discovered some fun combinations of cmdlets I thought I’d share.

First, let’s start with a command that lists all of the mailboxes in a given server, sorted by size. There are two variants for ascending and descending, differing only in the inclusion of the -Descending paramter in the Sort-Object cmdlet in the latter:

Get-MailboxStatistics -Server RED-MSG01 | Sort-Object -Property TotalItemSize | `
  Format-Table DisplayName,TotalItemSize

Get-MailboxStatistics -Server RED-MSG01 | Sort-Object -Property TotalItemSize -Descending | `
  Format-Table DisplayName,TotalItemSize

Pretty simple stuff, really; your basic pipeline exercise. You can also use Get-Mailbox -Server SERVER | Get-MailboxStatistics like I originally was, but since the Get-MailboxStatistics cmdlet already supports the -Server parameter it would be redundant. If you have some other way you need to define your collection of mailboxes, once you figure out how to define it, pipe the collection into Get-MailboxStatistics and away you go.

Next up, a variant of the same, only this time I want to only show those mailboxes larger than, say 1GB:

Get-MailboxStatistics -Server RED-MSG01 | Where {$_.TotalItemSize -gt 1GB} | `
  Sort-Object -Property TotalItemSize -Descending | Format-Table DisplayName,TotalItemSize

The Where cmdlet gives me access to comparisons against any propertDispy on the collection of objects passed into it, so be careful where you use it in the pipeline. The collection of objects given by Get-Mailbox is not the same collection of objects given by Get-MailboxStatistics, and they will have different sets of properties. And yes, you put multiple Where cmdlets into different places in your pipeline to fine-tune your selections, as the following search for the Kelly brothers illustrates:

Get-Mailbox -Server RED-MSG01 | Where {$_.DisplayName -imatch "kelly"} | `
  Get-MailboxStatistics | Where {$_.TotalItemSize -gt 1GB} | `
  Sort-Object -Property TotalItemSize -Descending | Format-Table DisplayName,TotalItemSize

This would show me all mailboxes whose display name contains the string “kelly” (without considering the case of the characters) and that was larger than 1GB in size. The -imatch comparison is a case-insensitive regular expression match, so I could have gotten fairly fancy in my comparison.

Last one: let’s take the list of mailboxes, in ascending order, and feed it to the Move-Mailbox cmdlet. This way, we’ll be moving the smaller mailboxes first, allowing us to get through more moves in the beginning and saving the real packrats for last:

Get-MailboxStatistics -Server RED-MSG01 | Sort-Object -Property TotalItemSize | `
  Move-Mailbox -TargetDatabase "RED-MSG02\Mailbox Database"

Let me know if you have questions or comments!