I've decided to start a new series on the blog called EWS Basics where I'll post some reusable scripts that are fit for easy customization by those people unfamiliar with EWS. So the basic idea is to have a group of samples where most of the underlying EWS plumbing code is done and you can just plug in whatever customized action you want to do on particular Items
Enumerate all the Items in any folder in a Mailbox
A common task when you want to report on the Item content in a Folder or you want to perform some type of action on Items (Delete,Move,Copy etc) is you will want to enumerate all the items in a folder. One analogy is doing a dir on directory (just don't mention the M: drive!)
So what this sample does is lets you input the MailboxName (PrimarySMTP Address) and Path to the Folder in the mailbox where the Item are you want to access and it will write back to the pipeline the Items it finds.
So to put that together in a sample say you want to enumerate the Items in the Clutter Folder and output the ReceivedDateTime and Subject you could use the following
Get-FolderItems -MailboxName firstname.lastname@example.org -FolderPath \clutter | Select DateTimeReceived,Subject
If the folder you wanted to access was a subfolder of the Inbox you could use
Get-FolderItems -MailboxName email@example.com -FolderPath \Inbox\Subfolder| Select DateTimeReceived,Subject
Another example of doing something a little more advanced is where you modify the script to produce statistics of the domains of the senders for email in a Mailbox folder. To do this you need to modify the part of the script that enumerates each of the Items so instead of writing the object to the pipeline it does some summarizations. I've added some lines in that uses a HashTable to compile the statistics on Email Domains and then writes out and sorts those values in the last line.
I've posted this script on GitHub https://github.com/gscales/Powershell-Scripts/blob/master/EnumerateItemsInFolder.ps1