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:

https://technet.microsoft.com/en-us/library/dd298173(v=exchg.160).aspx

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 https://outlook.office365.com/powershell-liveid/ -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 “admin@dagfriden.se” -TargetFolder “Item-search01” -LogLevel Full -Confirm:$false -DeleteContent -Force
}

 

 

 

Leave a Reply

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