The Search-Mailbox cmdlet can be used to perform various searches within a mailbox, and while it serves many needs, there are some searches that it can’t do. I wrote a PowerShell script to demonstrate how to perform a search of a mailbox (or mailboxes) using EWS, in this case looking for items of a particular message class. The script will search through all the folders of the mailbox (from the Top of Information store) and log all items found to a text file.
The search can be modified quite easily by changing the parameters in the SearchFolder function (modify $searchFilter accordingly), and also any processing can be added to the ProcessItem function (this function is called for every item that matches the search – in the sample, this is the function that logs to the text file).
The EWS Managed API needs to be installed on the machine running the script, but it does not need to be run on an Exchange server.
Script updated 10/8/2018 to add support for piping output to other PowerShell cmdlets (e.g. Export-CSV).
To list any items that match the message class:
.\Search-MailboxForMessageClass <mailbox smtp address> <message class>
To delete any found items:
.\Search-MailboxForMessageClass <mailbox smtp address> <message class> -DeleteItems
To export all contacts from a mailbox to a CSV file:
.\Search-MailboxForMessageClass.ps1 <mailbox smtp address> "IPM.Contact" -Credentials (Get-Credential) | Export-CSV "c:\temp\contacts.csv"
Please download the current version of the script from the Technet Code Gallery: https://gallery.technet.microsoft.com/Search-mailbox-for-items-a5a45dba