PowerShell: Stop Outlook from Junking Legit Messages

Outlook often struggles with erroneous junk mail classifications. It seems that some messages can pass through the SMTP perimeter scanning and email authentication, but still end up in Outlook’s junk folder.

The internal workings of Outlook’s built-in message filters aren’t fully documented anywhere that I know of. This can lead to some perplexing situations. Last week my own company’s newsletter was being routed to the junk folder. This was out of the blue, the monthly messages had never been considered SPAM before?

I went to some drastic lengths trying to determine why an internal message was triggering Outlook’s junk filter. I enabled logging, reviewed mail traces, and all the rest to no avail. The only way I could get the message to stay in the inbox was to add the entire sending domain to the safe list.

To run the script below you need the Exchange Online Management module installed. You also need access to credentials with the appropriate permissions in Microsoft 365 and Exchange Online. If the script is going to be your permanent solution, I suggest adding it to your onboarding processes.

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline

$trustedDomain = ” sender domain.com”

$mailboxes = Get-Mailbox -ResultSize Unlimited

Foreach ($mailbox in $mailboxes) {
    $currentTrustedSenders = Get-MailboxJunkEmailConfiguration -Identity $mailbox.UserPrincipalName | Select-Object -ExpandProperty TrustedSendersAndDomains
    if ($currentTrustedSenders -notcontains $trustedDomain) {
        $newTrustedSenders = $currentTrustedSenders + $trustedDomain
        Set-MailboxJunkEmailConfiguration -Identity $mailbox.UserPrincipalName -TrustedSendersAndDomains $newTrustedSenders
        Write-Output “Added $trustedDomain to trusted senders list for $($mailbox.UserPrincipalName)”
    } else {
        Write-Output “$trustedDomain is already in the trusted senders list for $($mailbox.UserPrincipalName)”
    }
}

Leave a comment