How to open windows firewall ports with powershell RRS feed

  • Question

  • Hi 

    Im trying to figure how how to make a Powershell script that opens, multiple firewall ports (incomming). on the local server. (using win2008r2).

    any ideas?

    best regards


    Monday, January 14, 2013 9:22 AM


All replies

  • Thanks,

    My problem is that my script is creating 3 seperate rules, one per port. I would love to have all the ports into one rule. Under is my current script:

    # 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
    $port2 = New-Object -ComObject HNetCfg.FWOpenPort
    $port3 = New-Object -ComObject HNetCfg.FWOpenPort

    $port1.Port = 109
    $port2.Port = 110
    $port3.Port = 111

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

    $port1.Enabled = $true
    $port2.Enabled = $true
    $port3.Enabled = $true

    $fwMgr = New-Object -ComObject HNetCfg.FwMgr



    This creates 3 rules with the same name. Is there anyway to get them all into one rule?



    Monday, January 14, 2013 11:28 AM
  •  netsh advfirewall firewall add rule name='MyTestPort' dir=in protocol=any localport=109-111 action=allow profile=any
    Monday, January 14, 2013 12:00 PM
  • Hi,

    Please also refer to the below link:

    Using Powershell to Open Ports on on a Windows 2008 Server


    Yan Li

    Cataleya Li
    TechNet Community Support

    Wednesday, January 16, 2013 6:10 AM