Home » DevOps » Batch file to open a specific TCP port in Windows 7 / 2008 Server and up

About Jeroen Pluimers

Jeroen Pluimers

Batch file to open a specific TCP port in Windows 7 / 2008 Server and up

Thanks to the answer by Kevin Richardson on How to open ports on Windows firewall through batch file, I wrote this batch file that uses the add command of the Netsh AdvFirewall Firewall Commands which requires Admin privileges to run:

:: open port (first argument passed to batch script, second argument is description)

:checkPrivileges
  net file 1>nul 2>nul
  if '%errorlevel%' == '0' ( goto :gotPrivileges ) else ( goto :getPrivileges )

:isNotAdmin
:getPrivileges
  echo You need to be admin running with an elevated security token to run %0
  goto :exit

:isAdmin
:gotPrivileges
  netsh advfirewall firewall add rule name="Open Port %1 for %2" dir=in action=allow protocol=TCP localport=%1

:exit
  ::pause
  exit /b
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a sysadmin Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Introduction to NGINX
2. Apache HTTP Server Cookbook
3. VirtualBox Essentials
4. Nagios Monitoring Cookbook
5. Linux BASH Programming Cookbook
6. Postgresql Database Tutorial
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of