Showing results for 
Show  only  | Search instead for 
Did you mean: 
Level 2

Enable/open outbound firewall ports using powershell script

We have created powershell script to open/enable outbound firewall ports instead of executing .bat file using Installshield 2015 professional edition. When we execute .bat file it will command prompt(i.e. Black window). We have created powershell script to hide command prompt window. But, rules are not created in outbound it always created in inbound rules.

Could you please anyone tell me how to created outbound ports using powershell script?

Labels (1)
0 Kudos
(3) Replies
Level 13

It looks like you have to specify the direction.
Open a powershell window and type in 'netsh advfirewall firewall add rule /?'
This should give you details. Look at the dir=in|out parameter. I suspect that you are missing dir=out. There are other parameters you may need to consider here as well.

Hopefully this helps.

If you are calling a .NET method then look for the equivalent there as well.

EDIT: I just looked up the .NET method using the INetFwPolicy2 to create a firewallpolicy. Here is some sample code:

INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID(FWPOLICYNAME));
INetFwRule firewallRule = firewallPolicy.Rules.OfType().Where(x => x.Name == policyName).FirstOrDefault();
firewallRule.LocalPorts = portNumber;

public const string FWPOLICYNAME = "HNetCfg.FwPolicy2";

add using NetFwTypeLib;
0 Kudos
Level 2

Thanks for the Reply.

But we have written script in powershell. Please find below code...!

$port1 = New-Object -ComObject HNetCfg.FWOpenPort
$port2 = New-Object -ComObject HNetCfg.FWOpenPort
$port3 = New-Object -ComObject HNetCfg.FWOpenPort
$port4 = New-Object -ComObject HNetCfg.FWOpenPort
$port5 = New-Object -ComObject HNetCfg.FWOpenPort
$port1.Port = 55007
$port1.Protocol = 6
$port2.Port = 55008
$port2.Protocol = 6
$port3.Port = 55004
$port3.Protocol = 17
$port3.Direction = 2
$port4.Port = 43440
$port4.Protocol = 6
$port5.Port = 43440
$port5.Protocol = 17
$port5.Direction = 2
$port1.Name = 'MA' # name of Port
$port1.Enabled = $true
$port2.Name = 'MA' # name of Port
$port2.Enabled = $true
$port3.Name = 'MA' # name of Port
$port3.Enabled = $true
$port4.Name = 'MA' # name of Port
$port4.Enabled = $true
$port5.Name = 'MA' # name of Port
$port5.Enabled = $true
$fwMgr = New-Object -ComObject HNetCfg.FwMgr

But in above code "$port5.Direction = 2" is not working.

Could you please help me to create/open firewall port in outbound rules using above code?

0 Kudos
Level 13

I really do not mind helping out, but I would suggest that you could have easily fixed this with some google searches.

You need to replace the '2' value with 'Inbound' or 'Outbound'. The PS documentation indicates that these are the acceptable values.
Specifies that matching firewall rules of the indicated direction are created.
This parameter specifies which direction of traffic to match with this rule.
The acceptable values for this parameter are: Inbound or Outbound.
The default value is Inbound.

See for a list of the PowerShell options for New-NetFirewallRule.
0 Kudos