Powershell script to count number of Mailbox folders in Exchange 2010

Hi Experts

I have a simple script that will tell me the number of folders in a users mailbox here:

(get-mailboxfolderstatistics -id %username%).count

Is there anyway to amend this or does anyone have a script that will list all users on the Exchange server and the number of folders each has.

Many thanks
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

I use this command to report on mailbox size and statistics for all users:
Get-MailboxStatistics -server SERVERNAME| Sort-Object TotalItemSize –Descending | ft DisplayName,TotalItemSize,ItemCount >C:\mailbox.txt

you should be able to incorporate your script into this?
Here you go. Just remember to change the servername in the first line.
This will sort output based on number of folders.

$mailboxes = get-mailbox -server SERVERNAME
$result = @{"DeclarationOfHashtable" = "To be removed"}
$mailboxes | foreach-object{
$result.add($_.Alias, (get-mailboxfolderstatistics $_).count)
$result.GetEnumerator() | sort value -descending

Open in new window

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
We can use something like the script I posted here:

$Mailboxes = Get-Mailbox -ResultSize unlimited
$Output = @()
foreach ($Mailbox in $Mailboxes){
  $MailboxStatistics = $Mailbox | Get-MailboxStatistics
  $FolderStatistics = $Mailbox | Get-MailboxFolderStatistics
  $objUser = New-Object System.Object
  $objUser | Add-Member -MemberType NoteProperty -Name "Name" -Value $Mailbox.Name
  $objUser | Add-Member -MemberType NoteProperty -Name "SamAccountName" -Value $Mailbox.SamAccountName
  $objUser | Add-Member -MemberType NoteProperty -Name "FolderCount" -Value $FolderStatistics.count
  $objUser | Add-Member -MemberType NoteProperty -Name "ItemCount" -Value $MailboxStatistics.ItemCount
  $objUser | Add-Member -MemberType NoteProperty -Name "TotalItemSize" -Value $MailboxStatistics.TotalItemSize.value.ToMB()
  $Output += $objUser

$Output | Select-Object Name,SamAccountName,FolderCount,ItemCount,TotalItemSize | Export-CSV "Output.csv"

Open in new window

forstersAuthor Commented:
Thanks both

Dnesse I could not  manipulate your script to work although it is a useful script so thanks

Thomas-Mjelde your script worked perfectly.  Many thanks
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.