Manage Office 365 global spam filter

First, connect to your tenant

Get-HostedContentFilterPolicy |fl

We need to manage the AllowedSenderDomains  field.

Issue this command to see the situation …

Get-HostedContentFilterPolicy |fl AllowedSenderDomains

If you want to expant it do this:

Get-HostedContentFilterPolicy | select -ExpandProperty AllowedSenderDomains 

To filter it and show only the domains list use this command:

Get-HostedContentFilterPolicy | select -ExpandProperty AllowedSenderDomains | Select-Object @{Name=’Domain’;Expression={$_}}

And now, we can export it to a CSV file to edit..

This is the command:

Get-HostedContentFilterPolicy | select -ExpandProperty AllowedSenderDomains | Select-Object @{Name=’Domain’;Expression={$_}} | Export-Csv .\PermitDomainList.csv -NoType

Now, after you have edited your csv list, we are ready to import it again ….

Now Import the CSV with this command:

Create a variable :

$AllowedSenderDomainList = Import-Csv .\PermitDomainList.csv

ForEach ($AllowedSenderDomain in $AllowedSenderDomainList) {Set-HostedContentFilterPolicy “Default” -AllowedsenderDomains @{Add=$} -MatchSubDomains True}

Now we can check the results:

Get-HostedContentFilterPolicy | select -ExpandProperty AllowedSenderDomains

This is the official Microsoft link about this command

That’s it!


  1. Thanks for the article! Any idea how to rename an existing policy from Powershell?

  2. Hi Marco, thanks for the content. Maybe you know the answer to this… when creating a policy by using new-hostedcontentfilterpolicy, the action succeeds, but the policy is ineffective as there is no tenant domains to which it is applied. Creating policy interactively in the UI requires the selection of tenant domains, but the Powershell version of creating the policy doesn’t have a parameter/attribute for this. Have you found a solution to creating a new policy using powershell and designating the appropriate tenant domains?

  3. I wish this worked.

    When trying to use this command.

    ForEach ($AllowedSenderDomain in $AllowedSenderDomainList) {Set-HostedContentFilterPolicy “Default” -AllowedsenderDomains @{Add=$} -MatchSubDomains True}

    I get this result.

    Cannot process argument transformation on parameter ‘AllowedSenderDomains’. Cannot convert value
    “System.Collections.Hashtable” to type “Microsoft.Exchange.Data.MultiValuedProperty`1[System.String]”. Error:
    “MultiValuedProperty collections cannot contain null values.


Leave a Reply

Your email address will not be published. Required fields are marked *