I started this series http://msmvps.com/blogs/richardsiddaway/archive/2011/07/30/outlook-connector-amp-mail-folder-item-count.aspx by looking at how we could enumerate the mail folders in Outlook 2010 when I had had four hotmail accounts open. The function has been modified since then
function get-mailitemcount { param ( [parameter(ParameterSetName="All")] [switch]$all, [parameter(ParameterSetName="Deleted")] [switch]$deleted, [parameter(ParameterSetName="Junk")] [switch]$junk ) $outlook = New-Object -ComObject Outlook.Application foreach ($folder in $outlook.Session.Folders){ foreach($mailfolder in $folder.Folders ) { if ($deleted) {if ($($mailfolder.Name) -notlike "Deleted*"){continue} } if ($junk) {if ($($mailfolder.Name) -notlike "Junk*"){continue} } New-Object -TypeName PSObject -Property @{ Mailbox = $($folder.Name) Folder = $($mailfolder.Name) ItemCount = $($mailfolder.Items.Count) } | select Mailbox, Folder, ItemCount } } }
I use three parameters to determine if I want to look at the Junk or Deleted folders or if I’m going to dump the information for all folders. I’ve used parameter sets to make the three parameters mutually exclusive.
The same loops are used as before – the outer one loops through the mailboxes and the inner one through each folder in those mailboxes. If the deleted or junk switches have been set any folder that doesn’t match the criteria is skipped.
An object is created to output the name of the mailbox, folder and number of items