Search and Delete an Email from office365.

Here is the steps to find and delete a specific mail from mailbox(s) from the office365 exchange.

1. Install PowerShell 7 using the following command:  winget install –id Microsoft.Powershell –source winget . Because complaince task need new PS.

2. PowerShell 7 will install side-by-side with your current version of Powershell. You will be able to find it using Search or in Start->All Programs. Start it

3. Install the Exchange Online Management Module using the command : install-module exchangeonlinemanagement

4. Connect to Exchange Online using the command: connect-exchangeonline. You will be asked to authenticate using your credentials ( Make sure the account that using has proper permission)

5. Connect to Security and Compliance Online using the command:  Connect-IPPSSession. You will be asked to authenticate using your credentials

6. Create a new compliance search: New-ComplianceSearch -Name “Give it a title” -ExchangeLocation All -ContentMatchQuery ‘(Received>=10/22/2020 -AND Received<=10/25/2020) AND (Subject:”provide words/phrase to look for in the subject”) AND (From:sender email address)’

7. Start the search with the command: start-compliancesearch “use the title you gave it above”

8. Check on the status of the search with the commanD: get-compliancesearch “user the title you gave it”. You can also use the command – get-compliancesearch “user the title you gave it” | fl, for more details and find out if any emails were found. You will not see a list in the results, but just a number.

9. If there were emails and you want to delete them then use the command: New-ComplianceSearchAction -SearchName “provide the title from above” -Purge -PurgeType SoftDelete

10. Check on the status: Get-ComplianceSearchAction “the title from above and append _purge”

References: 

1. https://learn.microsoft.com/en-us/powershell/exchange/connect-to-scc-powershell?view=exchange-ps 

2. https://learn.microsoft.com/en-us/purview/ediscovery-search-for-and-delete-email-messages

3. https://adamtheautomator.com/office-365-delete-email/