Counting Messages in an Exchange 2007 Mailbox Folder
Posted by Ben Lye in Exchange, Exchange 2007, Outlook, Tutorials on 19-01-2010
Tags: Exchange, Exchange 2007, messages, Outlook
I recently had a problem where a particular Outlook 2007 plug-in was causing sent e-mail messages to become “stuck” in the Outlook Outbox folder. The messages had actually been sent, and the “stuck” messages were only visible in Outlook Web Access or non-cached mode. So that we could remove the messages I needed to find out how many users where affected and how many messages those users had in their Outbox folders.
Fortunately the Exchange Management Shell provides cmdlets for answering these questions, and it can all be done with a PowerShell one-liner. While I was interested in counting messages specifically in the Outbox folder, the same technique could be used to count messages in any mailbox folder.
The first step is to get all the mailboxes with the Get-Mailbox cmdlet and sort them alphabetically by display name with the Sort-Object cmdlet:
Get-Mailbox -ResultSize unlimited | Sort-Object –Property DisplayName
Note: The mailboxes can also be filtered by specifying filters for the Get-Mailbox command
Next we want to use the Get-MailboxFolderStatistics cmdlet to get a count of the messages in the Outbox:
Get-Mailbox -ResultSize unlimited | Sort-Object –Property DisplayName | Get-MailboxFolderStatistics -FolderScope Outbox
Note: Items in other folders can be counted by changing “-FolderScope Outbox” as necessary.
We only want to show results for mailboxes with messages in the default Outbox folder, so we use the Where-Object cmdlet to filter the results:
Get-Mailbox -ResultSize unlimited | Sort-Object -Property DisplayName | Get-MailboxFolderStatistics -FolderScope Outbox | Where-Object {$_.ItemsInFolder -gt 0 -and $_.FolderType -ne "User Created"}
Lastly we can use Format-Table to format the output to show a simple table of mailbox names, item counts, and folder sizes. To do this we need to use an expression to call Get-Mailbox to get the mailbox name and another expression to convert the folder size to KB:
Get-Mailbox -ResultSize unlimited | Sort-Object -Property DisplayName | Get-MailboxFolderStatistics -FolderScope Outbox | Where-Object {$_.ItemsInFolder -gt 0 -and $_.FolderType -ne "User Created"} | Format-Table @{expression={Get-Mailbox ($_.Identity.ToString().Substring($_.Identity.ToString().LastIndexOf("/")+1,$_.Identity.ToString().IndexOf("\")-$_.Identity.ToString().LastIndexOf("/")-1))};label="Mailbox"}, ItemsInFolder, @{expression={$_.FolderSize.ToKB()};label="FolderSize (KB)"} -Autosize
Note: The above command is intended to be run as a single line of PowerShell, so beware any line-wrapping if you copy and paste it.
great article.
i have the same problem, any idea of why messages are going to the outbox instead of going to sent items?
what plugin caused your issue?
The problem in our case was caused by the PGP Desktop plug-in for Outlook.
Ben