Find specific text in the body or subject of an email using Microsoft PowerShell

Problem:

How do I find specific text in the body or subject of an email using Microsoft PowerShell

Solution:

First you will need to find the name of your DiscoveryMailbox:

Get-Mailbox RecipientTypeDetails DiscoveryMailbox | export-csv c:\temp\disc.csv 

 

Then run the below PowerShell code:

$UserCredential = Get-Credential

$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $UserCredential -Authentication "Basic" -AllowRedirection

Import-PSSession $exchangeSession -DisableNameChecking  

Get-Mailbox  -ResultSize Unlimited | Search-Mailbox -SearchQuery 'Subject:"*Some Text*"' -targetmailbox "DiscoverySearchMailbox{Your ID number}" -targetfolder "SearchResult" -logonly -loglevel full

Get-Mailbox  -ResultSize Unlimited | Search-Mailbox -SearchQuery 'Body:"*Some Text*"' -targetmailbox "DiscoverySearchMailbox{Your ID number}" -targetfolder "SearchResult" -logonly -loglevel full

 

You then need to add the DiscoverySearchMailbox to your Outlook to see the results.

Leave a Reply

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