How to create an outbound rule for a program via the command line?

I have several programs that I'd like to block from accessing the Internet. A manual (i.e. point-and-click) solution is described here.

Is there a way to do it via the command line on Windows 10?

Something like this pseudocode:

firewall --create --outbound --name "myRule" --target "C:\some\program.exe"

It could be either CMD or PowerShell.


Solution 1:

With Powershell, try the following:

New-NetFirewallRule -Direction Outbound -Program “C:\some\program.exe” -Action Block -Profile All -DisplayName “Block My Program.exe” -Description “Block My Program.exe” 

This will block for all profiles: Domain, Private and Public. If you want to block only for a particular profile, change -Profile All to -Profile Public if you want to block for public networks or -Profile Domain, Private if you want to block for domain and private networks.

If you don't have the New-NetFirewallRule commandlet, install the NetSecurity module:

Install-Module -Name NetSecurity