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
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 -loglevel full.
You can also use subject for SearchQuery
$mbx | Search-Mailbox -SearchQuery "Subject:This is Test Msg" -TargetMailbox “AdminMailbox” -TargetFolder "DeleteReport" -DeleteContent –Force -LogLevel Full