How to bulk add SPAM white and black list in Exchange Online Protection?


I hope you have a text files with SPAM white and black lists. If so, it’s all down to run the PowerShell command after connecting to Exchange Online.

If you created custom SPAM filter policy, replace “default” in Identity property with your custom filter policy name in the commands below.

Create different text files for Allowed and Blocked email addresses. Also create different text files for allowed and blocked email domain names.

The first line should be “Recipients” in all the text files.

Add bulk add SPAM White List (email addresses)

Import-Csv “C:\..\AllowedEmails.csv” | foreach {Set-HostedContentFilterPolicy -Identity Default -AllowedSenders @{add=$_.Recipients}} 

Add bulk add SPAM Block List (email addresses)

Import-Csv “C:\..\BlockedEmails.csv” | foreach {Set-HostedContentFilterPolicy -Identity Default –BlockedSenders @{add=$_.Recipients}} 

Add bulk add SPAM White List (email domains)

Import-Csv “C:\..\AllowedDomains.csv” | foreach {Set-HostedContentFilterPolicy -Identity Default –AllowedSenderDomains @{add=$_.Recipients}} 

Add bulk add SPAM Block List (email addresses)

Import-Csv “C:\..\BlockedEmails.csv” | foreach {Set-HostedContentFilterPolicy -Identity Default –BlockedSenderDomains @{add=$_.Recipients}} 

I hope it saved you sometime on your research.  Leave me a reply if it did.

13 thoughts on “How to bulk add SPAM white and black list in Exchange Online Protection?

  1. Definitely save me time! Just might have to “Enable-OrganizationCustomization” before you run. But it will say so if you need to do it.

  2. Skype has opened its web-structured client beta to the entire world, soon after introducing it broadly from the
    U.S. and U.K. earlier this calendar month.

    Skype for Website also now works with Chromebook and Linux
    for instant messaging conversation (no video and voice yet, these call for a connect-in installation).

    The increase from the beta provides help for a longer set of languages to
    help you bolster that international usability

      1. Thank you, so regarding the CSV file. I have typed “recipients” in cell A1 and the domain names in cell A2. Each domain name is separated by a “,”. So for example test.com,test1.com
        Is this correct. Also will it over write any allow domain that are already added previously? I.e will it append the new CSV list of domain names to what’s already configured? Also will powershell error if my new list of domains has a duplicate domain name to what’s already configured in the allow domain names.
        Thanks 🙂

  3. Hello
    Got it working but, i cant seem to use a comma. My cvs looks like the below and works and apends

    “recipients”
    domain1.com
    domain2.com
    domain3.com

    Any idea how to have the domains on one line? comma isnt working

  4. Thanks … When I put it in a script and call it from powershell session, I get the error:
    A positional parameter cannot be found that accepts argument ‘â€BlockedSenders @{add=@{Recipients=123@345.com}.SpamEmailAddresses}}

    The script is like:
    # Blocks Spammers – Email Address & Domains if required
    $Spammers = “E:\scripts\Repository\Exchange-Online\Blocked-Email-Address.csv”
    $SpamDomains = “E:\scripts\Repository\Exchange-Online\Blocked-Domains.csv”
    $SpamPolicy = ‘Custom-BlockList-For-AntiSPAM’
    # Block Senders
    Import-Csv $Spammers | foreach {Set-HostedContentFilterPolicy -Identity $SpamPolicy –BlockedSenders @{add=$_.SpamEmailAddresses}}
    # Block Domains
    Import-Csv $SpamDomains | foreach {Set-HostedContentFilterPolicy -Identity $SpamPolicy –BlockedSenderDomains @{add=$_.SpamDomains}}
    # End of Script

    The file E:\scripts\Repository\Exchange-Online\Blocked-Email-Address.csv has:
    SpamEmailAddresses
    Ryan@gotrenchless.com

    However when the commands are run individually in powershell, it works!

  5. it would help to know how exactly to export an existing spam policy so it matches for an import into another spam policy. I can get the import to work when i have a hand made csv BUT cannot get powershell to export a correctly exported csv with Get-HostedContentFilterPolicy it seems the delimiter is missing.

    1. You can try exporting to CSV like this:

      Get-HostedContentFilterPolicy -Identity ‘Standard Preset Security Policy1686347915304’ | select AllowedSenders | Export-Csv -Path .\AllowedSenders.csv -NoTypeInformation

Leave a reply to Antonetta Cancel reply