How Do I Open Ports with PowerShell?
If you have a history with Windows, you’re probably used to using netsh to open ports on the Windows Firewall. We have a whole slew of PowerShell cmdlets to administer the Windows Firewall now and I wondered how to use PowerShell to open ports. It took me longer than it should have to do it, so I thought I’d share. In the end it is simple, but there are a lot of cmdlets to wade through, which is where I got hung up.
Here is what you’d do with netsh to open ports 80 and 443:
netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80
netsh advfirewall firewall add rule name="Open Port 443" dir=in action=allow protocol=TCP localport=443
Here is how you’d open the same ports for only the Domain and Private profiles (not Public) with PowerShell:
New-NetFirewallRule -DisplayName 'HTTP(S) Inbound' -Profile @('Domain', 'Private') -Direction Inbound -Action Allow -Protocol TCP -LocalPort @('80', '443')