Windows – Is it possible to Block An Application from Accessing the Internet with cmd

batch filecommand linenetworkingwindows

Normally, I would use FireWall to Block An Application from Accessing the Internet. But I don't want to setup my FireWall system for just an application.

What I want is: create an bat file to start an application. The bat file similar to this.

If I run the bat file, the application is blocked from accessing internet. If I run the application directly, the application can access internet.

Is it possible to do that?

Best Answer

You could add firewall rule to block any incoming and outgoing traffic before running the application and disable (or delete) the firewall rules after exiting.

Setup block rules

:: Variables
set RULE_NAME=TemporaryBlock
set PROGRAM=C:\Program Files (x86)\App\app.exe

netsh advfirewall firewall add rule name="%RULE_NAME%" dir=in action=block profile=any program="%PROGRAM%"
netsh advfirewall firewall add rule name="%RULE_NAME%" dir=out action=block profile=any program="%PROGRAM%"

Run the app with internet blocked

@echo off

:: Variables
set RULE_NAME=TemporaryBlock
set PROGRAM=C:\Program Files (x86)\App\app.exe

:: Block the app
netsh advfirewall firewall set rule name="%RULE_NAME%" dir=in new enable=yes
netsh advfirewall firewall set rule name="%RULE_NAME%" dir=out new enable=yes

:: Running the app
start "" /wait "%PROGRAM%"

:: Disable the firewall rules
netsh advfirewall firewall set rule name="%RULE_NAME%" dir=in new enable=no
netsh advfirewall firewall set rule name="%RULE_NAME%" dir=out new enable=no

Details

  • documentation: netsh advfirewall firewall add rule -?
  • you could also create and delete the rules everytime you run the app, but I like creating the rules once and then just enabling or disabling it more
  • you need admin rights