Delete a specific email from all Users Mailboxes in O365.

Step 1 - Add your admin id to “Discovery Management” group
You can’t use Search-Mailbox cmd unless you have “Discovery Management” Group Rights, you can use EAC to Manage Role Group and Grant permission to your account.
Step 2 - To use -DeleteContent switch your account requires "Mailbox Import Export" role Access you can follow the below steps for the same.
  • Create a group Name Import and Export and add the admin account
New-RoleGroup “Import Export ” -Roles “Mailbox Import Export”
  • Add the admin ID to the role group.
Add-RoleGroupMember “Import Export” -Member “LABADMIN”
Step 3 - Run the below PowerShell to Remove the Messages
Below is the example of command I used where I had to delete email from the entire mailboxes in Organization from sender Example@gmail.com
To Query through all the mailboxes I created an array for all the mailboxes.
$mbx = Get-mailbox -resultsize Unlimited
Use below command to delete mail from all the mailboxes in organizations.
$mbx | Search-Mailbox -SearchQuery "from:Example@gmail.com" -TargetMailbox “AdminMailbox” -TargetFolder "DeleteReport" -DeleteContent –Force -LogLevel Full
Note: By default –loglevel value is set to Basic if you want full details or message delete use -log level full.
You can also use the subject for SearchQuery
$mbx | Search-Mailbox -SearchQuery "Subject:This is Test Msg" -TargetMailbox “AdminMailbox” -TargetFolder "DeleteReport" -DeleteContent –Force -LogLevel Full

Comments

  1. Hello. I had to run this command for our on prem Exchange today and it cleaned a rouge message to over 1000 users. However when I run this command in O365 with your instructions, I get an error that Search-Mailbox is not recognized. I have loaded the Exchange cmdlets in 0365 as I can do a Get-Mailbox and it returns the list of users. Any ideas.

    Thank you.

    Ken M

    ReplyDelete
  2. Hi. Disregard my previous comment. After I re read the post and saw your instructions at the beginning to add my Global admin to the Discovery Role Group, and create the import export group and assign my admin to that role, the command ran. Thank you for the helpful post.

    KenM

    ReplyDelete

Post a Comment