This is the Powershell version on adding CONNECTION FILTER IN MDO (Microsoft Defender for Office 365) https://security.microsoft.com/antispam
https://cp.hornetsecurity.com/manual/en/365_total_protection/t_deactivate_O365_spamfilter.html
RUN POWERSHELL "AS ADMIN"
Then copy each line 1 by 1
Set-ExecutionPolicy RemoteSigned (Press A then enter after)
Install-Module -Name ExchangeOnlineManagement (Press A then enter after)
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline > (Sign as the GLOBAL Admin of the Customer's M365 Tenant) if prompted "Sign in all apps and website on this device?" Select "No, this app only"
Set-HostedConnectionFilterPolicy -Identity Default -IPAllowList @{Add="83.246.65.0/24","94.100.128.0/24","94.100.129.0/24","94.100.130.0/24","94.100.131.0/24","94.100.132.0/24","94.100.133.0/24","94.100.134.0/24","94.100.135.0/24","94.100.136.0/24","94.100.137.0/24","94.100.138.0/24","94.100.139.0/24","94.100.140.0/24","94.100.141.0/24","94.100.142.0/24","94.100.143.0/24","173.45.18.0/24","185.140.204.0/24","185.140.205.0/24","185.140.206.0/24","185.140.207.0/24","108.163.133.224/27","199.27.221.64/27","209.172.38.64/27","216.46.2.48/29","216.46.11.224/27"}
Disconnect-ExchangeOnline -Confirm:$false (This command will end ExchangeOnline Powershell Session)
NOTE: This IP range list include US and CANADA
NOTE: base on the Article Enabling Organization Customization Your tenant must not be "Dehydrated" so that you can add CONNECTION FILTER
Command for US IP Address only:
Set-HostedConnectionFilterPolicy -Identity Default -IPAllowList @{Add="83.246.65.0/24","94.100.128.0/24","94.100.129.0/24","94.100.130.0/24","94.100.131.0/24","94.100.132.0/24","94.100.133.0/24","94.100.134.0/24","94.100.135.0/24","94.100.136.0/24","94.100.137.0/24","94.100.138.0/24","94.100.139.0/24","94.100.140.0/24","94.100.141.0/24","94.100.142.0/24","94.100.143.0/24","173.45.18.0/24","185.140.204.0/24","185.140.205.0/24","185.140.206.0/24","185.140.207.0/24"}
Now if the client wishes to remove these IP address, you just need to change the condition from ADD to REMOVE while still keeping any additional IP address remaining on the list.
Set-HostedConnectionFilterPolicy -Identity Default -IPAllowList @{Remove="83.246.65.0/24","94.100.128.0/24","94.100.129.0/24","94.100.130.0/24","94.100.131.0/24","94.100.132.0/24","94.100.133.0/24","94.100.134.0/24","94.100.135.0/24","94.100.136.0/24","94.100.137.0/24","94.100.138.0/24","94.100.139.0/24","94.100.140.0/24","94.100.141.0/24","94.100.142.0/24","94.100.143.0/24","173.45.18.0/24","185.140.204.0/24","185.140.205.0/24","185.140.206.0/24","185.140.207.0/24","108.163.133.224/27","199.27.221.64/27","209.172.38.64/27","216.46.2.48/29","216.46.11.224/27"}