How to open ports on Windows firewall through batch file

Stephane Grenier picture Stephane Grenier · Mar 2, 2013 · Viewed 37.7k times · Source

Is there any way within a batch file to open up specific ports on Windows through a batch file? It would be nice to have the installer do this for our server application rather than having the user manually do it.

Answer

Kevin Richardson picture Kevin Richardson · Mar 2, 2013

Use netsh.exe. A very simple batch file that takes a port argument:

@echo off
rem -- open port (first argument passed to batch script)
netsh advfirewall firewall add rule name="Open Port %1" dir=in action=allow protocol=TCP localport=%1 remoteip=10.15.97.0/24,10.17.0.0/16