How To Add A Port In Windows Firewall Using PowerShell? Print

  • 0

PowerShell is an advanced version of the command prompt. It has a large set of ready-to-use cmdlets and intelligence for using the .NET framework and C# in various scenarios. It can be used to open or close a port in Windows Server OS and Windows Desktop OS.

You need to know a few things to open ports on Windows Server/Windows systems. As

-> In which profile should the port be opened (Public, Private or Domain)? - Optional

-> Which port do you need to open (port number)?

-> Port direction - inbound (ie incoming requests) or outbound (ie outgoing requests).

-> Protocol by name (TCP, UDP, ICMPv4 or ICMPv6) or number (0-255).

Below are the steps on how to add a port in the Windows firewall using Powershell

Step 1: Search PowerShell in the Windows Server/Windows system. Once you get the result, right-click on it and select the option Run as administrator to open the application with administrator rights.

Step 2: Enter the following command in the PowerShell to open the port in the Firewall.

Here, we will create a firewall rule to allow the input on the TCP port 80 using PowerShell.

New-NetFirewallRule -DisplayName "ALLOW TCP PORT 80" -Direction inbound -Profile Any -Action Allow -LocalPort 80 -Protocol TCP

To open multiple ports, you need to enter the following command.

New-NetFirewallRule -DisplayName "Required Port" -Direction inbound -Profile Any -Action Allow -LocalPort 80,8080,8090 -Protocol TCP

Step 3: Once you fire the command in the PowerShell, you will get the following result which means that the port is successfully opened.

Step 4: If you want to verify the Firewall rule you created, you need to enter the following in Powershell.

$rule = "ALLOW TCP PORT 80"

Get-NetFirewallRule -DisplayName $rule | ft -Property Name, DisplayName, @{Name='Protocol';Expression={($PSItem | Get-NetFirewallPortFilter).Protocol}}, @{Name='LocalPort';Expression={($PSItem | Get-NetFirewallPortFilter).LocalPort}}, @{Name='RemotePort';Expression={($PSItem | Get-NetFirewallPortFilter).RemotePort}}, @{Name='RemoteAddress';Expression={($PSItem | Get-NetFirewallAddressFilter).RemoteAddress}}, Enabled, Profile, Direction, Action

Below is the result of the above command.

Let's verify the same rule in the Firewall settings

Step 5: Type Windows Firewall with Advanced Settings in the Search box. Once you type it, you will get the result below the Search box.  Click on it to open Windows Firewall with Advanced Settings window.

Step 6: In the Windows Firewall with Advanced Security window, click on the Inbound Rules.

Step 7: You can see the newly added firewall rule in the Inbound Rules section.

That's all.


Was this answer helpful?

« Back

chat