PowerShell: Search mailbox for items of a particular message class (ItemClass) No ratings yet.

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


Please rate this

6 thoughts on “PowerShell: Search mailbox for items of a particular message class (ItemClass)

  1. Hi,
    I am using the script to get Messages from Exchange Online that have the CommVault Stub. This is my Syntax:
    .\Search-MailboxForMessageClass.ps1 user@host.com “ipm.note.commvault.galaxy.stub” -Credentials (Get-Credential) -EwsUrl:https://outlook.office365.com/EWS/Exchange.asmx

    I have installed the EwsManagedApi v2.2.

    The Problem I have: I cannot read the Output of the script. I get Output like this:


    POST /EWS/Exchange.asmx HTTP/1.1
    Content-Type: text/xml; charset=utf-8
    Accept: text/xml
    User-Agent: ExchangeServicesClient/15.00.0516.014
    Accept-Encoding: gzip,deflate

    And then with EWS Request.

    What is wrong?


    1. I suspect that the script on this page had a bug, as I hadn’t updated it for a while. I moved it to the Technet Code Gallery some time ago and forgot to update the version here. I have now included a link to the current version of the script, so please try that and report back if you still have issues (you can provide feedback here or on Technet).

      1. Hi David,
        thank you for your answer.
        Thank you for the updated script link.
        It works but does not find a single item of the message class I entered in the Syntax.
        Do I understand it correctly that the script is using EWS to connect with OWA to search for the message class?

        My Output is like this:
        Search-MailboxForMessageClass.ps1 version 1.1.2 starting
        Using EWS Managed API found at: C:\Program Files (x86)\CVArchiverAddin\Microsoft.Exchange.WebServices.dll
        Processing mailbox user@domain.com
        Processing folder: \Oberste Ebene des Informationsspeichers
        Processing folder: \Oberste Ebene des Informationsspeichers\Archiv
        Processing folder: \Oberste Ebene des Informationsspeichers\Aufgaben
        Processing folder: \Oberste Ebene des Informationsspeichers\Conversation Action Settings
        Processing folder: \Oberste Ebene des Informationsspeichers\Dateien
        Processing folder: \Oberste Ebene des Informationsspeichers\Inbox

        I am pretty sure that the user Mailbox has more than thousand stubs.

        If I open the Mailbox of the user with OWA and open a mail with a stub, I can open the source code of the mail. In that source code, I can find several message class entries but no “ipm.note.commvault.galaxy.stub” message class.

        Maybe you have an idea how to get these mails with stub.

        Thank you very much and kind regards,


Leave a Reply

Your email address will not be published. Required fields are marked *