How to open a firewall port in Windows using Power Shell

36,880

You can refer to the guide here.

The command to open port 80 is:

netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80

You need to specify:

  • name for the rule
  • direction
  • whether to allow the connection
  • protocol used
  • port number

You can use this command from the Powershell level.

If you absolutely must use Powershell, you can use something like the script below(for the port 80 as well):

#==============================================================
# Creates a rule to open an incomming port in the firewall.
#==============================================================

#$numberAsString = read-host "type an port number"
#$mynumber = [int]$numberAsString


$port1 = New-Object -ComObject HNetCfg.FWOpenPort

$port1.Port = 80

$port1.Name = 'MyTestPort' # name of Port

$port1.Enabled = $true

$fwMgr = New-Object -ComObject HNetCfg.FwMgr

$profiledomain=$fwMgr.LocalPolicy.GetProfileByType(0)

$profiledomain.GloballyOpenPorts.Add($port1)

Taken from here.

Share:
36,880

Related videos on Youtube

justin
Author by

justin

Updated on September 18, 2022

Comments

  • justin
    justin over 1 year

    I would like to know how to open a firewall port in Windows by using Power Shell.Could anyone write a script for opening a firewall port.I saw a similar post on https://stackoverflow.com/questions/24760821/changing-windows-firewall-rules-with-powershell-open-close-a-specific-port but couldn't understand how to do it.

    I would just want to open a port:8983 in windows because when I execute the application(stack dump) it says pysolr.SolrError: Failed to connect to server at 'http://localhost:8983/solr/stackdump/admin/ping', are you sure that URL is correct?.Atlast it says:No connection could be made because the target machine actively refused it.

  • justin
    justin over 9 years
    :Thanks.I tried it and it worked but fortunately it says:The requested operation requires elevation (Run as administrator).I'm using this computer from college library so may be that might be the problem.
  • mnmnc
    mnmnc over 9 years
    Well, I think You might be right. The college library computers, or pretty much any publicly available computers are known to have limitations implemented that will prohibit any changes to the computer configuration. I'm pretty sure that above scripts will work on normal computers.
  • justin
    justin over 9 years
    :I would also like to ask can we execute the script on linux?Becuase while I'm running the application XOWA it says that the `connection is refused' in firefox while using an XOWA addon for firefox.Do these type of errors happen because a port is not opened?
  • mnmnc
    mnmnc over 9 years
    Connection is refused looks more like the response from the destination server or from the network device that is between you and destination server. Linux is a different operating system. Pretty much totally different. You cannot execute above comments in Linux. There are Linux commands what will do the samem but this is entirely different question.
  • justin
    justin over 9 years
    :Have you heard about XOWA?It's really an application which can allow you to have wikipedia offline.XOWA addon in firefox helps us to run the application from firefox.But still as you said the 'destination server' here that's really my computer itself.So I think the problem would be with the port beause the response should go through a port,isn't it?
  • mnmnc
    mnmnc over 9 years
    Well, not really. If the destination is your computer locally, that means the connection source and destination is the IP address 127.0.0.1 (most likely) which is local IP address of each computer. This IP address is attached to loopback device that have very little in common with normal ethernet card. Firewall rules do not normally affect the localhost so there shouldn't be anything that would prevent you from connecting.
  • justin
    justin over 9 years
    :Yeah that's right.But can we enter into localhost(127.0.0.1) without any internet connection?The problem is I can't even enter localhost.But after connecting the XOWA application to browser I can enter into it and see wikipedia.But the process of retriving images from XOWA is very slow that'w why I'm trying to use offline wikipedia in browser using XOWA addon which uses XOWA protocol.So the problem is that it(firefox browser) says CONNECTION IS REFUSED.