Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

How to Enable a Port Through PowerShell

Open a TCP or UDP port in Windows Firewall with New-NetFirewallRule, limit who may connect, test it properly from another machine, and remove the rule again.

3 min readBy Soumya

Reviewed by the AmazingRDP technical team

Enabling a port through PowerShell

Hosting an application that needs its own port? A Windows VPS gives you a dedicated IP address and full control of the firewall. See Windows VPS from $8.99

A program on your Windows server listens on a port, and nobody outside can reach it: Windows Firewall blocks incoming connections unless a rule allows them. One PowerShell command adds that rule. This works on Windows 10 and 11 and on Windows Server 2012 R2 and later.

1. Open PowerShell as administrator

Right-click the Start button and choose Windows PowerShell (Admin) or Terminal (Admin).

2. Allow incoming connections on the port

For TCP port 8080:

New-NetFirewallRule -DisplayName "Allow TCP 8080" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow

The rule works at once; no restart is needed. For UDP, change the protocol:

New-NetFirewallRule -DisplayName "Allow UDP 8080" -Direction Inbound -Protocol UDP -LocalPort 8080 -Action Allow

Several ports, or a range, in one rule:

New-NetFirewallRule -DisplayName "Allow web ports" -Direction Inbound -Protocol TCP -LocalPort 80,443,8080 -Action Allow
New-NetFirewallRule -DisplayName "Allow 8000-8100" -Direction Inbound -Protocol TCP -LocalPort 8000-8100 -Action Allow

3. Limit who may connect

A port that is open to the whole internet gets scanned within minutes. If only you or one office need it, name the address:

New-NetFirewallRule -DisplayName "Allow TCP 8080 from office" -Direction Inbound -Protocol TCP -LocalPort 8080 -RemoteAddress 203.0.113.10 -Action Allow

You can also limit a rule to network profiles, for example -Profile Domain,Private, so that it does not apply on public networks.

4. Check the rule

Get-NetFirewallRule -DisplayName "Allow TCP 8080" | Get-NetFirewallPortFilter

This shows the protocol and port the rule covers.

5. Test it properly

A firewall rule only lets traffic through. Something must also be listening on the port. On the server:

Get-NetTCPConnection -LocalPort 8080 -State Listen

If this prints nothing, the program is not running or uses another port. Then test from another computer:

Test-NetConnection -ComputerName 198.51.100.20 -Port 8080

Use the server's address. TcpTestSucceeded : True means the port is reachable. Testing against localhost on the server itself proves nothing about the firewall, because local connections do not pass through it.

Do I need an outbound rule?

Usually not. Windows Firewall allows outgoing connections by default, and replies to an allowed incoming connection pass automatically. You need an outbound rule only if outgoing traffic has been set to "block" on your server. In that case the rule names the port on the other side:

New-NetFirewallRule -DisplayName "Allow out to TCP 8080" -Direction Outbound -Protocol TCP -RemotePort 8080 -Action Allow

Switch a rule off, or remove it

Disable-NetFirewallRule -DisplayName "Allow TCP 8080"
Enable-NetFirewallRule -DisplayName "Allow TCP 8080"
Remove-NetFirewallRule -DisplayName "Allow TCP 8080"

The old netsh command

On very old systems, or in batch files, the same rule is:

netsh advfirewall firewall add rule name="Allow TCP 8080" dir=in action=allow protocol=TCP localport=8080

Still not reachable?

  • Another firewall in front. A router, a cloud firewall or the provider's network filter may block the port before it reaches Windows.
  • The program listens only locally. If it is bound to 127.0.0.1, it accepts connections from the server itself only. Change its setting to listen on all addresses.
  • A block rule. In Windows Firewall, a rule that blocks wins over a rule that allows.
  • Shared RDP. Firewall rules need administrator rights, and a shared RDP has one address for many users. Use an admin RDP or a Windows VPS.

To see what is actually connecting once the port is open, read how to monitor all network traffic in Windows. Related: set .ps1 files to open with PowerShell.

Questions

How do I open a port in Windows Firewall with PowerShell?

Run New-NetFirewallRule -DisplayName "NAME" -Direction Inbound -Protocol TCP -LocalPort PORT -Action Allow as administrator.

How do I check if a port is open?

From another computer, run Test-NetConnection -ComputerName SERVER -Port PORT. A program must be listening on the port for the test to succeed.

Do I need to restart after adding a firewall rule?

No. The rule applies immediately.

Can I open several ports with one rule?

Yes. List them with commas, as in -LocalPort 80,443,8080, or give a range such as 8000-8100.