Set-DistributionGroup HiddenFromAddressListsEnabled Error
Why Microsoft 365 won't let you hide a synced group from address lists, and how to fix it on-premises with msExchHideFromAddressLists and a sync.
How to search for and delete email messages across Microsoft 365 mailboxes with PowerShell, updated for modern authentication and the 2025 purge changes.

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.
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 *
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).
New-ComplianceSearchAction returns an error.-Scope CurrentUser avoids that. PowerShell 7 or Windows PowerShell 5.1 both work.Treat this as an example and test it against one mailbox before using it organisation-wide.
Install-Module ExchangeOnlineManagement -Scope CurrentUser
# or, if already installed:
Update-Module ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
Connect-IPPSSession -UserPrincipalName admin@yourdomain.com -EnableSearchOnlySession
New-ComplianceSearch -Name "NameOfTheSearch" -ExchangeLocation All -ContentMatchQuery 'subject:"Exact subject here"'
Start-ComplianceSearch -Identity "NameOfTheSearch"
Get-ComplianceSearch -Identity "NameOfTheSearch" | Format-List Name,Status,Items
If the item count looks higher than you expect, stop and refine the query.
New-ComplianceSearchAction -SearchName "NameOfTheSearch" -Purge -PurgeType SoftDelete
Get-ComplianceSearchAction -Identity "NameOfTheSearch_Purge" | Format-List
Disconnect-ExchangeOnline -Confirm:$false
Get-ComplianceSearchAction should show the purge as completed.If the connection itself fails, Microsoft's troubleshooting page advises updating the V3 module to the latest version and trying again.
| 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.
📝 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.