Office 365: Search for and Delete Email Messages

How to search for and delete email messages across Microsoft 365 mailboxes with PowerShell, updated for modern authentication and the 2025 purge changes.

Office 365: Search for and Delete Email Messages

Sometimes an email reaches far more mailboxes than it should: a message sent to the wrong distribution list, or a phishing email that slipped through. Asking every user to delete it is slow and unreliable. In 2021 I wrote a short note on how to search for and delete email messages across Office 365 (now Microsoft 365) with PowerShell. The core of it still works, but the way I connected no longer does, so here is the full method with updates.

The original method (2021)

This is what worked for me at the time. I opened Windows PowerShell as admin and connected to the Security & Compliance Center like this:

$credentials = Get-Credential
Connect-ExchangeOnline -Credential $credentials
$SccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $credentials -Authentication "Basic" -AllowRedirection
Import-PSSession $SccSession

Then, against an existing content search, I ran either a soft or hard delete and checked progress:

New-ComplianceSearchAction -SearchName "NameOfTheSearch" -Purge -PurgeType SoftDelete
New-ComplianceSearchAction -SearchName "NameOfTheSearch" -Purge -PurgeType HardDelete
Get-ComplianceSearchAction
Get-ComplianceSearchAction 'NameOfTheSearch_Purge' | Select *

Why the connection part no longer works

Update: Microsoft's connection guide now covers only the Exchange Online PowerShell V3 module, using modern authentication and MFA. The New-PSSession with -Authentication "Basic" approach has been retired, and Get-Credential does not sit well with MFA.

There is also a newer requirement. To run purge actions you need ExchangeOnlineManagement version 3.9.0 or later. You must also connect with Connect-IPPSSession using the -EnableSearchOnlySession switch. Microsoft announced this in Message Center post MC1131771, effective 26 September 2025. You do not need Connect-ExchangeOnline for this job at all.

The purge and progress commands themselves are unchanged. One small correction: the action name is NameOfTheSearch_Purge (the format is the search name followed by _Purge).

Before you start

  • Permissions: you need the Search And Purge role in the Microsoft Purview portal (the renamed Security & Compliance Center). You can have it directly or through a role group such as Organization Management. Without it, New-ComplianceSearchAction returns an error.
  • Mailboxes only: if your search includes SharePoint or OneDrive sites, the purge action fails.
  • Run interactively: these cmdlets do not support certificate-based authentication, so unattended scripts are not an option.
  • Admin rights: you only need them to install the module for all users. -Scope CurrentUser avoids that. PowerShell 7 or Windows PowerShell 5.1 both work.

How to search for and delete email messages today

Treat this as an example and test it against one mailbox before using it organisation-wide.

  1. Install or update the module.
Install-Module ExchangeOnlineManagement -Scope CurrentUser
# or, if already installed:
Update-Module ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
  1. Connect with modern authentication and the search-only switch.
Connect-IPPSSession -UserPrincipalName admin@yourdomain.com -EnableSearchOnlySession
  1. Create and run the search. Make the query as narrow as you can, for example an exact subject.
New-ComplianceSearch -Name "NameOfTheSearch" -ExchangeLocation All -ContentMatchQuery 'subject:"Exact subject here"'
Start-ComplianceSearch -Identity "NameOfTheSearch"
  1. Check the results before deleting anything.
Get-ComplianceSearch -Identity "NameOfTheSearch" | Format-List Name,Status,Items

If the item count looks higher than you expect, stop and refine the query.

  1. Purge. SoftDelete leaves the message recoverable by the user; HardDelete does not. I start with SoftDelete unless there is a clear reason not to.
New-ComplianceSearchAction -SearchName "NameOfTheSearch" -Purge -PurgeType SoftDelete
  1. Monitor progress and disconnect.
Get-ComplianceSearchAction -Identity "NameOfTheSearch_Purge" | Format-List
Disconnect-ExchangeOnline -Confirm:$false

How to check it worked

  • Get-ComplianceSearchAction should show the purge as completed.
  • Spot-check one or two affected mailboxes, ideally including your test mailbox.
  • Ask a couple of recipients to confirm the message has gone from their inbox.

If the connection itself fails, Microsoft's troubleshooting page advises updating the V3 module to the latest version and trying again.

Cautions checklist

Check Why
Item count reviewed A broad query can delete mail across the whole organisation
Tested on one mailbox Confirms the query matches only what you intend
SoftDelete first Leaves a recovery path for users
Retention and legal hold considered Deleting evidence may conflict with your policies
Change recorded Keeps an audit trail of who purged what and why
Module version checked Purge needs 3.9.0 or later

This area has changed before and will change again. Check the Microsoft Learn guide on finding and deleting messages and recent Message Center posts before you rely on any script. If you would rather have help handling incidents like this, my company Standing provides information security and managed services in Iraq and the Kurdistan Region.

The takeaway: the purge commands are the same as in 2021, but connect with Connect-IPPSSession -EnableSearchOnlySession. Never run a purge until you have checked the item count.

Sources & further reading

  1. Microsoft Learn – Find and delete email messages in eDiscoverylearn.microsoft.com
  2. Microsoft Learn – New-ComplianceSearchActionlearn.microsoft.com
  3. Microsoft Learn – Connect to Security & Compliance PowerShelllearn.microsoft.com
  4. Microsoft Learn – Connect-IPPSSessionlearn.microsoft.com
  5. Microsoft Learn – Can't connect to Security & Compliance PowerShelllearn.microsoft.com
  6. Message Center MC1131771 summarypurview.expert

📝 First written from my own work in 2021; updated and expanded in November 2026 with AI assistance, keeping my original fix. Spotted something wrong? Tell me.

Akam Omer
Written by

Akam Omer

IT leader, engineer and security specialist from Erbil with 20+ years across higher education, telecoms, healthcare and government. Founder of BulkSMSIraq, VerifyWay and Kurd.ai.