Combine Get-MailboxFolderStatistics and Get-Mailbox

Hey folks.

I've recently been asked to slightly amend a simple Exchange Management Shell one-liner I put together a while ago.

It goes like this:

"ServerName" | Get-MailboxServer | Get-Mailbox -ResultSize unlimited | Get-MailboxFolderStatistics | where {$_.ItemsInFolder -gt 4700} | Sort-object -descending ItemsInFolder | ft Identity, FolderPath, ItemsInFolder, @{Label="FolderSize(MB)";expression={$_.FolderSize.ToMB()} }, @{Label="FolderSize(MB)";expression={$_.FolderAndSubfolderSize.ToMB()} } -a | out-file C:\ServerName-HighItemCount.txt -width 750

It will get the mailbox server, get each mailbox on said server and then get mailbox folder statistics where ItemsInFolder is greater than 4700 items.  It'll sort the output descending by ItemsInFolder and dump out several other properties of the object.

However, what Get-MailboxFolderStatistics does not do is show the Database or StorageGroup property of the mailbox.  So, is there a way I can sort combine the two to show the StorageGroup property of the mailbox in question??  Perhaps with a variable?

Any help is much appreciated.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:

Yes, you can. Although I don't believe in one-liners I'm afraid, too messy :)

Get-Mailbox -ResultSize Unlimited -Server "ServerName" | %{
  $Mailbox = $_
  Get-MailboxFolderStatistics $_.Identity | ?{ $_.ItemsInFolder -gt 4700 } | `
    Select-Object Identity, FolderPath, ItemsInFolders, `
      @{n='FolderSize(MB)';e={ $_.FolderSize.ToMb() }}, `
      @{n='FolderAndSubfolderSize(MB)';e={ $_.FolderAndSubfolderSize.ToMb() }}, `
      @{n='Database';e={ $Mailbox.Database }}

Or something like that, can't test it here, and I forget if "Get-Mailbox" exposes a StorageGroup Property.

It only uses ForEach-Object (%) because it leaves output in the pipeline. That lot be piped into whatever formatting options you like, whether that's Export-CSV, ConvertTo-Html, Format-Table, Out-GridView, or something else entirely. Just tack that onto the very end.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mawatsonAuthor Commented:
That was a stroke of genius, Chris-Dent.  Nice one.

I took what you proposed and morphed it a bit to get this:

"ServerName" | Get-MailboxServer | Get-Mailbox -ResultSize unlimited | %{
$Mailbox = $_
  Get-MailboxFolderStatistics $_.Identity | ?{ $_.ItemsInFolder -gt 100 } | `
    Select-Object Identity, FolderPath, ItemsInFolder, `
      @{n='FolderAndSubfolderSize(MB)';e={ $_.FolderAndSubfolderSize.ToMb() }}, `
      @{n='Database';e={ $Mailbox.Database }}
} | Sort-object -descending ItemsInFolder | ft -a | out-file C:\Server_FolderList.txt -width 750

Many many thanks for the feedback.  This will be a helpful primer as reference for combining Exch Mgmt Shell cmdlets.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.