{"id":3426,"date":"2024-06-21T12:47:09","date_gmt":"2024-06-21T17:47:09","guid":{"rendered":"https:\/\/microsoftgeek.com\/?p=3426"},"modified":"2024-06-21T13:05:48","modified_gmt":"2024-06-21T18:05:48","slug":"o365-delete-specific-email-from-sender-to-mailboxes","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=3426","title":{"rendered":"O365: Delete specific email from sender to mailboxes"},"content":{"rendered":"\n<p>Here&#8217;s a PowerShell script that uses the Exchange Online PowerShell module to delete a specific email from multiple mailboxes. This script searches for an email with the specified subject line sent from the specified sender and deletes it.<\/p>\n\n\n\n<p>Before running the script, ensure you have the Exchange Online PowerShell module installed and have the necessary permissions to search and delete emails in the target mailboxes.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>powershellCopy code<code># Import the Exchange Online module\nImport-Module ExchangeOnlineManagement\n\n# Connect to Exchange Online\nConnect-ExchangeOnline -UserPrincipalName your_admin_account@domain.com -ShowProgress $true\n\n# Define the sender and subject line\n$sender = \"username@company.com\"\n$subject = \"Subject line title\"\n\n# Create a unique name for the compliance search\n$searchName = \"DeleteSpecificEmailSearch\"\n$actionName = \"DeleteSpecificEmailAction\"\n\n# List of mailboxes to search\n$mailboxes = @(\"mailbox1@domain.com\", \"mailbox2@domain.com\", \"mailbox3@domain.com\")\n\n# Create a compliance search\nNew-ComplianceSearch -Name $searchName -ExchangeLocation $mailboxes -ContentMatchQuery \"From:$sender AND Subject:$subject\"\n\n# Start the compliance search\nStart-ComplianceSearch -Identity $searchName\n\n# Wait for the search to complete (adjust the sleep time as necessary)\nStart-Sleep -Seconds 60\n\n# Check the status of the search\n$searchStatus = Get-ComplianceSearch -Identity $searchName\nif ($searchStatus.Status -eq \"Completed\") {\n    # Create a compliance search action to delete the emails\n    New-ComplianceSearchAction -SearchName $searchName -Purge -PurgeType HardDelete\n    Write-Host \"Emails deleted successfully.\"\n} else {\n    Write-Host \"Search did not complete successfully. Please check the search status.\"\n}\n\n# Cleanup (optional)\n# Remove-ComplianceSearch -Identity $searchName\n# Remove-ComplianceSearchAction -Identity $actionName\n\n# Disconnect from Exchange Online\nDisconnect-ExchangeOnline -Confirm:$false\n<\/code><\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> Adjust the sleep time based on the expected time for the search to complete. You can also implement a loop to check the status periodically instead of using a fixed sleep time.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a PowerShell script that uses the Exchange Online PowerShell module to delete a specific email from multiple mailboxes. This script searches for an email with the specified subject line sent from the specified sender and deletes it. Before running the script, ensure you have the Exchange Online PowerShell module installed and have the necessary [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,91,90,60],"tags":[],"class_list":["post-3426","post","type-post","status-publish","format-standard","hentry","category-exchange-server-2019","category-m365","category-o365","category-office-365"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3426"}],"version-history":[{"count":6,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3426\/revisions"}],"predecessor-version":[{"id":3444,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3426\/revisions\/3444"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}