Delete spam mail from Exchange Online for all users

The Search-Mailbox command can me user to filter mails from Exchange online and delete unwanted mail from all users mailboxes.

Documentation for the command can be found here:

Example for searing all users mailboxes, copy items to the admin account and then delete the items from the mailboxes.

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

$Mailboxes = Get-Mailbox -resultsize unlimited
$SeachThisMailboxes = $Mailboxes | where {$_.PrimarySmtpAddress -notlike “amin*”}
$SeachThisMailboxes | ForEach-Object{
Write-Host $_.Alias
Search-Mailbox -Identity $_.Alias -SearchQuery ‘Subject:”Delat dokument” AND “Vänliga hälsningar,” AND “Jag har delat ett dokument med dig på OneDrive.”‘ -TargetMailbox “” -TargetFolder “Item-search01” -LogLevel Full -Confirm:$false -DeleteContent -Force




Leave a Reply

Your email address will not be published. Required fields are marked *