Everybody’s mailbox is flooded with junk emails by default. Luckily, Outlook and Exchange Online are getting better every year at detecting junk emails automatically. Users can also add email addresses manually to the blocker sender list or whitelist specific email addresses.
Now this list, the safelist collection, is stored at the mailbox level. Admins can of course block or allow email addresses on a tenant level, but sometimes you also want to check, add, or remove an email address at the mailbox level. This is where the Set-MailboxJunkEmailConfiguration cmdlet comes in.
In this article
In this article, I will explain how you can use the Set MailboxJunkEmailConfiguration cmdlet to manage the safelist collection of a mailbox.
Junk Email Settings Explained
Before we are going to take a look at how to add or remove email addresses to the safelist collection, let’s first take a look at the junk email settings and how it works.
The safelist collection is a list with all manually allowed and blocked recipients for a specific mailbox. The user can whitelist or block recipients in Outlook by right-clicking on an email and choosing Block > Block Sender or Never Block Sender.
We can also view the list in Outlook under Settings > Mail > Junk email:

When it comes to managing junk email, there are two scenarios that we need to distinguish first, Exchange On-Premises and Exchange Online. There is an important difference between the two that you will need to know.
With Exchange On-Premise, the safelist collection and the SCL Junk email threshold determine if an email is moved to the junk folder or not. We can disable the junk email rule on the mailbox level, to stop the delivery of mail in the junk folder completely.
However, if you are using Exchange Online, then not only the safelist collection determines if an email is moved to the junk folder, but also the anti-spam policies in Exchange Online Protection. We can still disable the junk email rule on the mailbox level, but this will only affect the safelist collection.
Viewing Trusted & Blocked Senders
Before we are going to make changes to the list, it is always a good idea to view get the current configuration. In PowerShell, we can use the Get-MailboxJunkEmailConfigation cmdlet to view the current configuration. Make sure that you are connected to Exchange Online before you proceed.
Get-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com
This will return the complete configuration, to view the trusted or blocked senders, we can simply select and expand the properties:
# View blocked senders Get-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com | Select -ExpandProperty BlockedSendersAndDomains # View trusted senders and output the results to gridview Get-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com | Select -ExpandProperty TrustedSendersAndDomains | Out-GridView
Adding an Email Address to Blocked Senders
I personally prefer to add blocked senders on a server level, because often when you need to block it for one user, more users will eventually follow. But we can add one or more email addresses to the blocked senders list in Outlook with the help of PowerShell.
Important here is that you use a hashtable with the Add key to add the email address. If you don’t do that, you will overwrite the existing list, thus removing any existing entries.
# Add an email address to the blocked sender list
Set-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com -BlockedSendersAndDomains @{Add="jane@fourthcoffee.com"}
# WRONG WAY
Set-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com -BlockedSendersAndDomains "jane@fourthcoffee.com"

We can also add one or more email addresses to the blocked sender list of all (or a selection) of mailboxes of course:
Get-ExoMailbox -ResultSize Unlimited | Set-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com -BlockedSendersAndDomains @{Add="jane@fourthcoffee.com", Add="john@contoso.com"}
Besides email addresses, we can also add complete domains to the list. Simply specify the domain name that you want to block like this:
Set-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com -BlockedSendersAndDomains @{Add="fourthcoffee.com"}
Adding Email Address to the Trusted Sender List
Adding one or more email addresses to the trusted sender list is pretty much the same principle as with the blocked senders:
Set-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com -TrustedSendersAndDomains @{Add="jane@fourthcoffee.com"}
You can also add an email address to the trusted list while simultaneously adding one to the blocked list by simply specifying both parameters:
Set-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com -TrustedSendersAndDomains @{Add="jane@fourthcoffee.com"} -BlockedSendersAndDomains {Add="john@contoso.com"}
And of course, we can also add domain names to the trusted sender list:
Set-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com -TrustedSendersAndDomains @{Add="microsoft.com"}
Removing Values from the Lists
We can also remove values from the Blocked and Trusted sender lists. You can use the same format to remove senders from the list, just change the key in the hashtable to Remove. Also good to know, that you can simultaneously add and remove senders from a list:
Set-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com -BlockedSendersAndDomains @{Remove="jane@fourthcoffee.com", Add="jan@thirdcoffee.com"}
If you want to find all mailboxes that have a particular email address in their blocked list, then you can use the following PowerShell command to find the mailboxes:
Get-ExoMailbox | Where-Object {($_ | Get-MailboxJunkEmailConfiguration).BlockedSendersAndDomains -contains "jane@fourthcoffee.com"}
Trusted Contacts
A commonly good idea is to automatically add contacts in your Outlook to your trusted sender lists. We can do this by simply setting the parameter ContactsTrusted to True in PowerShell:
Set-MailboxJunkEmailConfiguration -Identity lazyadmin@lazydev.onmicrosoft.com -ContactsTrusted $true
The power of PowerShell is that we can simply get all mailboxes in our tenant where the setting is disabled and enable it with a single command:
Get-MailboxJunkEmailConfiguration * | Where {$_.ContactsTrusted -eq $false} | Set-MailboxJunkEmailConfiguration -ContactsTrusted $true
Wrapping Up
The Set-Mailboxjunkemailconfiguration cmdlet is great when you need to clean up the safelist collection of a specific mailbox. But when it comes to adding email addresses to the block list, I personally prefer to do this on the Exchange server level. More than often you want to block the sender for everybody, not only for a single user.
Hope you liked this article, if you have any questions, just drop a comment below.



Enjoying the article but hate the ads? Join the Insiders for a clean, ad-free experience ($1), or go Elite ($3) to get priority answers and more.
Join LazyAdmin