Exchange PowerShell: How to check the number of items in the Inbox, Sent Items, Deleted Items and Junk Email

Exchange PowerShell

In this post we will look at how to find out how many items are in a users Inbox, Sent Items, Deleted Items and Junk Email.

Every so often I get a call from a user complaining that Outlook is slow while moving from/to/within their Inbox. More often than not it’s because their Inbox has 1000’s of items in it and the user has never done any house keeping to keep their mailbox in shape.

The PowerShell reports we are going to generate today can be used proactively to prompt users to do some house keeping on their mailbox before they experience a problem.

Firstly we have to find out which PowerShell Cmdlet to use. So in an Exchange Management Shell lets start with the Cmdlet:

Get-Help *folder*

get-help folderAs you can see there are a few Cmdlets with folder in the name, but Get-MailboxFolderStatistics looks promising.

Next lets run the Cmdlet Get-MailboxFolderStatistics | Get-Member to see what information it will return us.

Get-mailboxfolderstatistics get-memberYou can see one of the properties is ItemsInFolder. You’ll also see that I had to specify an identity for the Cmdlet.

Next lets try a the Cmdlet with Select-Object.

Get-MailboxFolderStatistics | Select Name, ItemsInFolder

Get-mailboxfolderstatistics select name itemsinfolderAgain I had to specify an Identity, but it is returning the item count for each folder in the mailbox.

Now I want to trim this down to display the item count for the Inbox, Sent Items, Deleted Items and Junk Email only. To be able to do this we will need to perform a Where operation on the Name property, combined with match to match the string to the name.

Get-MailboxFolderStatistics | Where {$_.Name -match “Inbox|Sent Items|Deleted Items|Junk E-Mail”} | Select Name, ItemsInFolder

get-mailboxfolderstatistics Where Name match Inbox Sent Items Deleted Items Junk E-Mail Select Name ItemsInFolderNow I’d like to perform the same Cmdlet for all mailbox users on the server at the same time.

The command to return all mailboxes is Get-Mailbox.

get-mailboxHow about we try and pipe Get-Mailbox into Get-MailboxFolderStatistics?

Get-Mailbox | Get-MailboxFolderStatistics | Where {$_.Name -match “Inbox|Sent Items|Deleted Items|Junk E-Mail”} | Select Name, ItemsInFolder

get-mailbox get-mailboxfolderstatistics Where Name match Inbox Sent Items Deleted Items Junk E-Mail Select Name ItemsInFolderAs you can see it iterates through the mailboxes but the we don’t know which mailbox the item count corresponds to. Looking back at the Get-MailboxFolderStatistics | Get-Member Cmdlet, lets try using the Identity property as well.

Get-Mailbox | Get-MailboxFolderStatistics | Where {$_.Name -match “Inbox|Sent Items|Deleted Items|Junk E-Mail”} | Select Identity, Name, ItemsInFolder

get-mailbox get-mailboxfolderstatistics Where Name match Inbox Sent Items Deleted Items Junk E-Mail Select Identity Name ItemsInFolderThis now shows the Identity property, but not very well for on screen. To view it onscreen I’m going to change the Select Cmdlet for the Format-Table Cmdlet and use the -Autosize option.

Get-Mailbox | Get-MailboxFolderStatistics | Where {$_.Name -match “Inbox|Sent Items|Deleted Items|Junk E-Mail”} | FT Identity, Name, ItemsInFolder -Autosize

get-mailbox get-mailboxfolderstatistics Where Name match Inbox Sent Items Deleted Items Junk E-Mail formtat-table Identity Name ItemsInFolder -autosizeNow you can see the mailbox owner, folder and item count. To export this to a CSV file we revert back to using the Select Cmdlet.

Get-Mailbox | Get-MailboxFolderStatistics | Where {$_.Name -match “Inbox|Sent Items|Deleted Items|Junk E-Mail”} | Select Identity, Name, ItemsInFolder | Export-csv c:\temp\MailboxItemCount.csv

On hindsight although it’s nice to have all the data in one CSV I didn’t find it very useable. So instead I decided to break the Cmdlet down to look at individual Folders one at a time, and then sort the results in order of size.

For the Inbox:

Get-Mailbox | Get-MailboxFolderStatistics | Where {$_.Name -match “Inbox”} | Select Identity, Name, ItemsInFolder | Sort-Object ItemsInFolder -descending | Export-csv c:\temp\MailboxItemCountInbox.csv

For Deleted Items:

Get-Mailbox | Get-MailboxFolderStatistics | Where {$_.Name -match “Deleted Items”} | Select Identity, Name, ItemsInFolder | Sort-Object ItemsInFolder -descending | Export-csv c:\temp\MailboxItemCountDeletedItems.csv

The PowerShell command above works on both Exchange 2007 and Exchange 2010.

Related Posts:

1. How to install Exchange 2013 (SP1) on Windows Server 2012 R2

2. Exchange 2013 Initial Configuration Settings multi-part series

3.  Office 365 PowerShell: How to bulk change Office 365 calendar permissions using Windows PowerShell

4. Exchange PowerShell: How to list all SMTP email addresses in Exchange

5. Exchange PowerShell: How to enumerate and modify Distribution Group properties

If you found this post useful, please share!

    Related Posts

    7 thoughts on “Exchange PowerShell: How to check the number of items in the Inbox, Sent Items, Deleted Items and Junk Email

    1. Adam

      Really helpful post – however on my ItemsCount it keeps picking up hidden application folders (according to what TechNet Forums says..) so making the final count not a true representative of what is actually in the users mailbox..

      Any ideas on how to get round this?

      Reply
    2. Shudhanshu

      Very much helpful. Will you be able to tell me the command to find out folder count for list of mailboxes/users?

      Thanks,
      Sudhanshu

      Reply
    3. Jack

      First off great article it’s very helpful I just have one question. I have around 1900 mailboxes and keep getting the error that tells me only the first 1000 will be returned. So the error says use “-ResultSize Unlimited” and I tried that but can’t seem to make it happy in the context of the command you built up to here. Could you help me out with that?

      Reply
    4. Anders Heick

      Hi Carl,

      Thanks for the great examples. Be aware, that your code will not work if users have localized mailbox folders (which is quite common in non-english speaking countries – also mixed names across users are common), since you match on the Name property. Another option is to use the FolderType property instead – and if you are only interested in user mailboxes, you can add the RecipientTypeDetails filter as well. All combined here:

      Get-Mailbox -RecipientTypeDetails UserMailbox | Get-MailboxFolderStatistics | Where {$_.Foldertype-eq “DeletedItems”} | Select Identity, Name, ItemsInFolder

      Cheers,
      Anders

      Reply

    Leave a Reply

    Your email address will not be published. Required fields are marked *