We had a client that had the need for a mass import of whitelists and blacklists. A quick Google rendered a few results however each command failed for various reasons. We were in a time crunch so a call to Microsoft was in order. They provided us with a “newer” set of commands.
You’ll first need to create your domain or user-based safe sender or blocked sender list. See TechNet article below for those steps. https://docs.microsoft.com/en-gb/office365/SecurityCompliance/create-organization-wide-safe-sender-or-blocked-sender-lists-in-office-365?redirectedfrom=MSDN
Next build your CSV containing the email addresses and domain names or export from your current lists. Here’s a quick shot of what your CSV file should look like. NOTE: that the import will allow for “*.domain.com”.
Example CSV of addresses
Example CSV of domains
Connect to O365 via PowerShell
- Set-ExecutionPolicy Unrestricted (hit “Y” and enter)
- $LiveCred = Get-Credential
- $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri # -Credential $LiveCred -Authentication Basic -AllowRedirection
- Import-PSSession $Session
- Connect-MsolService -Credential $LiveCred
Please note that you’ll see the spilt import for email addresses and domain names below. Your transport rule and your CSV files must align accordingly.
Bulk import of Whitelist
Run the below PowerShell to allow the sender Address :
foreach ($content in (get-content “C:\whitelist.csv”)){$temp=(get-transportrule “Whitelist sender address matches”).FromAddressContainsWords;$temp+=$content;set-transportrule “Whitelist sender address matches” -FromAddressContainsWords $temp}
Run the below PowerShell to allow the senders domain :
foreach ($content in (get-content “C:\whitelist.csv”)){$temp=(get-transportrule “Whitelist 2”).FromAddressContainsWords;$temp+=$content;set-transportrule “Whitelist 2” -FromAddressContainsWords $temp}
Bulk import of Blacklist
Run the below PowerShell to block the senders address :
foreach ($content in (get-content “D:\blacklist_addresses.csv”)){$temp=(get-transportrule “Blacklist”).From;$temp+=$content;set-transportrule “Blacklist” -From $temp}
Run the below PowerShell to block the senders domain :
foreach ($content in (get-content “D:\blacklist_domain.csv”)){$temp=(get-transportrule “Blacklist Domain”).SenderDomainIs;$temp+=$content;set-transportrule “Blacklist Domain” -SenderDomainIs $temp}
Brandon Stuart, PEI