Powershell script to create inbox rule to move spam to junk folder on all Exchange 2010 mailboxes

With Hexamail Guard you can tag any email that is processed as spam using header or subject tags. The script below uses the simplest case (a subject prefix tag) to move spam to user “Junk E-mail” folders  automatically in Exchange.
Remember to turn off the Spam blocker Block and Delete options if you want all spam to pass through to the users’ mailbox junk folders.
#Get list of all Exchange mailboxes
$list = Get-Mailbox * -ResultSize unlimited

foreach ($entry in $list)
{
$user = $entry.alias
$check = Get-InboxRule -Mailbox $user | where { $_.name -like "Hexamail Spam" }

if ($check -ne $null)
{
continue
}
elseif ( $check -eq $null)
{
New-Inboxrule -name "Hexamail Spam" -mailbox $user -MoveToFolder "$($user):\Junk E-Mail" -SubjectContainsWords "Potential spam:" -ExceptIfSubjectContainsWords  ("RE: Potential spam:","FWD: Potential spam:") -confirm:$false -priority 999 -force
}
}
exit
Advertisements