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=$AllowedSenderDomain.name} -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!
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?
try this way : https://blog.ciaops.com/2018/06/25/configuring-an-office-365-spam-filtering-policy-with-powershell/
bye
Marco
I wish this worked.
When trying to use this command.
ForEach ($AllowedSenderDomain in $AllowedSenderDomainList) {Set-HostedContentFilterPolicy “Default” -AllowedsenderDomains @{Add=$AllowedSenderDomain.name} -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.
https://github.com/zackoch/Powershell-Administration/blob/master/spam_filter_export_import_allowed_domains.ps1
that should work better. The OP has a few mistakes in his syntax in the article.
Thanks Zac.
Great job!
Marco