Just recently, I was asked to help investigate which Exchange cmdlets would help a partner the I work with, do an item count in an on premises Exchange Public folder structure. Their specific ask was to get, per folder, the number of contact items.
So starting with the best command to do this, it’s easy to get to the conclusion that it will be the Get-PublicFolderItemStatistics, and the first thing that you need to know about that cmdlet is that it’s only available in Exchange 2010+.
The second thing you need to focus on is, in which folders do you want to run the count on? All of them? And if not all, do you want to run the count based on folder type? i.e do you want to just count calendar items on folders of type calendar? How can we achieve this?
Lets break this down:
- To be able to select the folders you want to count the items for, you need of course to start with the Get-PublicFolder cmdlet
- If you want to filter just one or multiple folder type (i.e Calendar, Contacts, etc) you need to do it using the “FolderClass” attribute.
Note: The “FolderClass” attribute doesn’t exist in all versions of Exchange. I haven’t checked in detail but at least apparently in Exchange 2010 you won’t be able to leverage this attribute to filter just the folders you want. Worst case scenario you can always run a count against all folders. Also note that as you can see below, not all folders have a “FolderClass”.
And finally the code to grab all the folders you want.
With the FolderClass attribute filtering:
Note: The Where-Object filtering (? sign in the command above) in PowerShell caches all its results into memory, so if you have a very large public folder structure you might want to have that in mind and run the commands in a machine with enough resources.
Now that we know how to grab all the folders we need, lets look at how to do the folder count:
- The command used to do the folder count is, as mentioned above in this post, the Get-PublicFolderItemStatistics
- Because all you want to do is count items of a certain type, you will leverage the “ItemType” attribute in your filtering
- Don’t forget that the Get-PublicFolderItemStatistics is an Exchange 2010+ cmdlet
Below see the output of an item count of a specific folder.
Now, finally, the entire script (in bold the item count):
- we start by getting all folders of class contact. Again you can do this filtering or not, depending on the Exchange version and what you need exactly.
- we then enter a loop where, for each folder, we will grab all items of type contact and count them
- once that is done we write the output into the console
This script is very simple and doesn’t have error handling, logging and output to CSV. If you want those features feel free to contact me via the blog and I can build you a very complete version of the script.
Running the simple version of the script in a large environment can make the results difficult or impossible to analyse, however, with the code above gives you an insight in how to filter and count Public folders, by type and class.
As always I hope this is helpful.