Managing Senders Using PowerShell

Gestion des expéditeurs

Managing Senders Using PowerShell

For PowerShell beginners, I recommend checking out this article .

The term “spam filter” defines an Exchange mail security mechanism implemented at the “mailbox level.”
In an Exchange-based environment, the mail security infrastructure is defined at the “server level” (Exchange server).

There is also an added layer of mail security at the mailbox level.

The additional component that can be managed by the Exchange recipient is the “spam engine”. It consists of the following two components:

  • The spam filter. This is the Exchange mailbox security filter. By inspecting each email message, it can decide whether the email is “spam” or not. By default, the mailbox spam filter is not active.
  • Spam filter lists. This is the Exchange mailbox security filter. It can classify a specific sender or domain as “legitimate sender” or “illegitimate sender.”
Gestion des expéditeurs

Exchange mailbox management options “Spam component”

Exchange mailbox management “Spam Component” is implemented at two levels:

  • User level – each Exchange recipient can manage their Exchange mailbox “Junk Component” using Outlook or OWA email settings.
  • Administrative level – using PowerShell

The other option to manage Exchange mailbox “Spam Component” is set on the “server side” using PowerShell.

The main advantages of using PowerShell to manage “Spam Filter” configurations are:

Using Bulk Configurations

The ability to implement a configuration setting required for a “large number of mailboxes” at the same time.

Executing specific configuration settings on behalf of the user

The ability of the Exchange Online administrator to perform a specific configuration setting. And by doing so, it avoids a scenario in which a non-technical user would have to perform the specific configuration settings.

Safe Senders List and Blocked Senders List

The Exchange “mailbox Junk Email Filter Lists” option allows Exchange users to filter email messages using four types of filters:

  • List of safe senders – represented by the PowerShell parameter – TrustedSendersAndDomain
  • Blocked Senders List – represented by the PowerShell parameter – BlockedSendersAndDomains
  • List of safe recipients – represented by the PowerShell parameter – TrustedRecipientsAndDomains
  • International – Currently, there is no PowerShell setting for this option. The “international” option allows us to block a specific email message based on a specific country domain name or language encoding.

Most of the time, the common options that users use are:

List of safe senders

The purpose of the safe senders list is to prevent false positives. These are legitimate emails mistakenly identified as spam.

When we add a domain name or email address to the safe senders list, an email is sent from that sender. Emails sent from that sender will never be sent to the spam folder.

List of blocked senders

The purpose of the blocked senders list is to prevent false negatives. These are spam emails that manage to bypass the existing security filter.

When we add an email address to the blocked senders list, that email is blocked. Emails sent from that particular sender will automatically be sent to the spam folder.

Messages from people or domain names on this list are always classified as spam, regardless of the content of the message.

Gestion des expéditeurs

How to define the “sending entity”?

To define the “sender” we use the sender’s domain name or email address.

In case we define a domain name, the filtering rule will be applied to all senders whose email address contains that specific domain name.

Outlook and spam protection

In the following screenshot, we can see the default configuration of Outlook and spam protection. By default, the Outlook spam filter is not active.

In other words, by default, Outlook does not use a local spam filter.
Instead, the classification of email as legitimate or spam will be implemented by the “server side” (Exchange server).

Gestion des expéditeurs

In the following screenshot we can see the different “list tabs” that we can use to define blocked or safe sender lists.

Gestion des expéditeurs

Section 1#4 – Adding Email Addresses and Domain Names to the Safe Senders and Blocked Senders Lists

Add email addresses and domain names to safe sender lists

Scenario 1.1 – Add email addresses and domain names to safe sender lists | Specific recipient

We would like to define a whitelist of trusted senders by adding a domain name and email address to the safe senders list.
In this scenario, we want to add this information to a specific Exchange recipient.

The parameter we use to define the information saved in the safe senders list is – TrustedSendersAndDomainswe use to define the information saved in the safe senders list

Set-MailboxJunkEmailConfiguration -Identity Brad@o365info.com -TrustedSendersAndDomains "cnn.com","alice@contoso.com"

Scenario 1.2 – Add email addresses and domain names to safe sender lists in bulk mode

We would like to define a whitelist of trusted senders by adding a domain name and email address to the safe senders list.
In this scenario, we would like to implement a “mass update” by adding the information to all existing Exchange recipients.

Get-Mailbox -ResultSize Unlimited | Set-MailboxJunkEmailConfiguration -TrustedSendersAndDomains "cnn.com","alice@contoso.com"

Add email addresses and domain names to blocked senders lists

Scenario 1.3 – Add email addresses and domain names to blocked sender lists | Specific recipient

We would like to set up a blacklist of untrusted senders by adding a domain name and email address to the blocked senders list.
email address to the blocked senders list.

In this scenario, we want to add the information to a specific Exchange recipient.
The parameter we use to define the information that has been saved in the safe senders list is – BlockedSendersAndDomainsx

	
 Set-MailboxJunkEmailConfiguration -Identity Brad@o365info.com -BlockedSendersAndDomains"spam.com","amanda@baddomain.com"

Scenario 1.4 – Add email addresses and domain names to the blocked senders list in Bulk mode

We would like to set up a blacklist of untrusted senders by adding a domain name and email address to the blocked senders list.
email address to the blocked senders list.

In this scenario, we would like to implement a “mass update” by adding the information to all existing Exchange recipients.

	
 Get-Mailbox -ResultSize Unlimited | Set-MailboxJunkEmailConfiguration -BlockedSendersAndDomains "spam.com","amanda@baddomain.com"

Scenario 1.5 – Add email addresses and domain names to the safe senders lists + blocked senders list at the same time | Bulk mode

We would like to define a combination where we add information (domain name and
email address) to the blocked senders list + the safe senders list.

Get-Mailbox -ResultSize Unlimited | Set-MailboxJunkEmailConfiguration -TrustedSendersAndDomains "cnn.com",alice@contoso.com -BlockedSendersAndDomains 
"spam.com","amanda@baddomain.com"

Section 2#4 – Additional Spam Filter Options

Enable or disable the spam filter

Scenario 2.1 – Enable the spam filter

We want to enable the mailbox spam filter for a specific recipient

Set-MailboxJunkEmailConfiguration "John@o365info.com" -Enabled $true

Scenario 2.2 – Disable the spam filter

We want to disable the mailbox spam filter for a specific recipient.

Get-MailboxJunkEmailConfiguration * | Where {$_.ContactsTrusted -eq $true} | Set-MailboxJunkEmailConfiguration -ContactsTrusted $True

Configure the trusted contact option

Another option we can use to define our safe senders list is to “ask” the mailbox spam filter list to automatically trust each of the recipients (email addresses) in our contact list. This option is called “Also trust emails from my contacts” and is not active by default.

Scenario 2.3 – Enable Trusted Contact Option | Bulk Mode

We would like to enable the “Also forward my contacts’ emails” option to all our recipients (Bulk mode).

Get-MailboxJunkEmailConfiguration * | Where {$_.ContactsTrusted -eq $true} | Set-MailboxJunkEmailConfiguration -ContactsTrusted $False

Scenario 2.4 – Disable trusted contact option in Bulk mode

We would like to disable the “Also forward emails from my contacts” option to all our recipients (Bulk mode).

 Get-MailboxJunkEmailConfiguration * | Where {$_.ContactsTrusted -eq $true} | Set-MailboxJunkEmailConfiguration -ContactsTrusted $False

Section 3#4 – Display information about safe senders and blocked lists

Scenario 3.1 – Display information about safe sender and blocked lists | Specific recipient

We would like to see the list of email addresses and domain names that appear on the safe senders and blocked senders lists.

Get-MailboxJunkEmailConfiguration -Identity Brad@o365info.com

Scenario 3.2 – View information about Safe sender and blocked lists | Bulk fashion

We want to display the list of email addresses and domain names that appear in the safe senders lists and blocked senders lists.

In this scenario, we would like to see information about all existing Exchange mailboxes.

Get-Mailbox -ResultSize Unlimited | Get-MailboxJunkEmailConfiguration

Export information about Safe sender and blocked lists | Bulk fashion

Scenario 3.3 – Export information to CSV file

We want to – export information from safe senders lists and blocked senders lists of all existing Exchange mailboxes to a CSV (comma separated value) file.

 Get-Mailbox -ResultSize Unlimited | Get-MailboxJunkEmailConfiguration | Export-CSV c:\temp\" Safe sender and blocked lists.CSV" –NoTypeInformation 
-Encoding utf8

Scenario 3.4 – Export information to TXT file

We want to – export information from Safe Senders Lists and Blocked Senders Lists of all existing Exchange mailboxes to a TEXT file.

 Get-Mailbox -ResultSize Unlimited | Get-MailboxJunkEmailConfiguration | Out-File c:\temp\"fe sender and blocked lists.txt" -Encoding UTF8

Section 4#4 – Updating (deleting and adding) existing safe and block sender lists

In this section, we’ll cover how to update allowed senders lists and blocked senders lists. In this scenario, we want to add or remove information from existing safe senders lists and blocked senders lists.

Updated existing safe sender lists with new information

Scenario 4.1 – Add E-mail address and domain names to Safe sender lists | Specific recipient

We would like to – add the email address and domain name to the current safe sender lists for a particular recipient.

 Set-MailboxJunkEmailConfiguration -Identity Brad@o365info.com -TrustedSendersAndDomains @{Add="fabrikam.com","Jeff@Trusteddomain.com"}

Scenario 4.2 – Add E-mail address and domain names to Safe sender lists | Bulk fashion

We would like to add an additional email address and domain name to the current safe sender lists of all Exchange recipients.

Get-Mailbox -ResultSize Unlimited | Set-MailboxJunkEmailConfiguration -TrustedSendersAndDomains @{Add="fabrikam.com","Jeff@Trusteddomain.com"}

Updated existing safe sender lists by removing information about existing senders

Scenario 4.3 – Remove email addresses and domain names from safe sender lists | Specific recipient

We would like to remove an existing safe sender email address and domain name for a particular user mailbox.

Set-MailboxJunkEmailConfiguration -Identity Brad@o365info.com -TrustedSendersAndDomains @{Remove ="fabrikam.com","Jeff@Trusteddomain.com"}

Scenario 4.4 – Remove email addresses and domain names from safe sender lists in bulk mode

We want to remove an existing safe sender email address and domain name for all Exchange recipients.

Get-Mailbox -ResultSize Unlimited | Set-MailboxJunkEmailConfiguration -TrustedSendersAndDomains @{Remove ="fabrikam.com","Jeff@Trusteddomain.com"}
How to prevent your emails from ending up in spam?

Prevent your emails from ending up in spam?

How to get the most out of Office 365 policies?

Microsoft Defender: Getting the Most Out of Office 365 Policies

How to manage spam using email flow rules?

Spam Filtering with Exchange Email Flow Rules

Scroll to Top