Importing Email Addresses & Domains To Blacklist In Office 365 ...
Maybe your like
Preparing the List
I recently had to migrate away from our third-party spam filter. I was able to get them to send us a CSV file that contains a list of all our blocked domains/email addresses, but that list was not very clean.
For example, it looked like this with a mix of domains and email addresses.

In order to make this work, I needed to clean up the list to extract emails to one file, and domains to another. To do that, I used the following commands within Kali Linux to make two new files; one that contains a list of emails, and a 2nd that contains a list of domains.
cat tmp.txt | grep @ | sort -u > emails.txt
cat tmp.txt | grep -v @ | sort -u > domains.txt

The PowerShell Section
Regardless which of the below scripts you use, you’ll need to connect to Exchange Online using something like the below.
$UserCredential = Get-Credential $Session = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential –Authentication Basic -AllowRedirection Import-PSSession $SessionImporting Domains to Block List
With our lists ready, I used the following code to import the domains.
$domains = "C:\temp\domains.txt" # EDIT THIS $count = 1 foreach ($content in ($total = get-content $domains)){ $totalcount = $total.count Set-HostedContentFilterPolicy -Identity Default –BlockedSenderDomains @{add=$content} write-host "Added $count entries of $totalcount : $content" $count += 1 }
Importing Emails to Block List
With our lists ready, I used the following code to import the email addresses.
$emails = "C:\temp\emails.txt" # CHANGE THIS $count = 1 foreach ($content in ($total = get-content $emails)){ $totalcount = $total.count Set-HostedContentFilterPolicy -Identity Default –BlockedSenders @{add=$content} write-host "Added $count entries of $totalcount : $content" $count += 1 }
How do we know this worked?
Let’s head over to the Exchange Admin Center. From there, we’ll go into Protection and find the Spam Filter.

Open up the Default policy and head over to the Block Lists. You should see your entries here.

Share this:
- X
Related
Tag » Add Domain To Whitelist Office 365 Powershell
-
How To Whitelist A Domain In Office 365 - LazyAdmin
-
[SOLVED] Whitelisting Domain On Exchange Via Powershell
-
How To Bulk Whitelist Domains In Office 365 Using Powershell
-
Add A Domain To A Client Tenancy With Windows PowerShell For DAP ...
-
Using Powershell To Whitelist Your Emails In M365 - KAMIND IT
-
How Do I Whitelist A Domain In Office 365? - Holm Security
-
Whitelist Domains And Email Addresses On Exchange Server And ...
-
Add Your Domain Name To Trusted Senders For All Delegated Office ...
-
Whitelist A Domain In Office 365 Exchange Online | HowTo
-
How To Create A Whitelist On Office 365 - Ery Bilişim
-
Adding A Domain To Office 365 With PowerShell - MSB365
-
Whitelist Domain In Office 365 To Bypass SPAM Filtering - ALI TAJRAN
-
Bulk Import Of Whitelist Or Blacklists Llists Into O365 - PEI
-
Configure Anti-spam Policies In EOP - Microsoft-365-docs - GitHub