How to open a port in the Windows Operating System using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To open a port in the Windows Operating system, we need to know a few things. Like

• For which Profile we need to open port (Public, private, or Domain)? - Optional

• Which port do we need to open (port Number)?

• The direction of the port – Inbound (i.e Incoming requests) or Outbound (i.e. Outgoing requests).

• Protocol by name (TCP, UDP, ICMPv4, or ICMPv6) or Number (0-255).

Once we have all the details we can open the port. In the below example, we need to open a port 5985 (WINRM HTTP) port on the computer which is currently blocked. So we will use the below command.

New-NetFirewallRule -DisplayName "Allow WINRM HTTP Port"
-Direction Outbound 
-LocalPort 5985
-Protocol TCP 
-Action Allow

To open multiple ports,

New-NetFirewallRule -DisplayName "Allow web ports"
-Direction Outbound 
-LocalPort 80,8080
-Protocol TCP 
-Action All



Once you open the Firewall settings, you can check the ports are listed in the allowed list.

To perform the same settings on the remote computer, you need to use the Invoke-Command cmdlet. Make sure that the remote computer is accessible and has the WINRM connectivity before opening the port on the remote computer.

Invoke-Command -ComputerName Test1-Win2k12 -ScriptBlock{
New-NetFirewallRule -DisplayName "Allow web ports"
-Direction Outbound 
-LocalPort 80,8080
-Protocol TCP 
-Action All
}

If we don’t specify Direction (Inbound or Outbound), both directions ports will be opened by default.

Published on 16-Oct-2020 13:01:46