Windows : How to open a port on firewall from command line (CLI)

By | December 13, 2021

Windows Firewall can be configured from the GUI (by using firewall.cpl UI console) and also using the command line. This quick tutorial will cover how to manipulate the rules from CLI to open, block a port and delete a rule.

In  a nutshell, here is the command sample:

netsh advfirewall firewall add rule name="<DESCRIPTION>" dir=in action=<ACTION> protocol=<PROTOCOL> localport=<PORT>

The rest of this article will assume we want to play with TCP port 3001.

1. Open port tcp-3001:

netsh advfirewall firewall add rule name="tcp-3001" dir=in action=allow protocol=TCP localport=3001

2. Block port tcp-3001:

netsh advfirewall firewall add rule name="tcp-3001" dir=in action=block protocol=TCP localport=3001

3. Delete the rule tcp-3001:

netsh advfirewall firewall delete rule name="tcp-3001" dir=in action=allow protocol=TCP localport=3001

4. In case you want to play with existing rules, here is the command to show all existing rules:

netsh advfirewall firewall show rule name=all

BONUS. Here is the commands to enable/disable the firewall:

netsh advfirewall set allprofiles state on
netsh advfirewall set allprofiles state off