Finding Open Firewall Ports

by Dec 7, 2018

Here is a piece of PowerShell code that connects to the local firewall and dumps the open firewall ports:

$firewall = New-object -ComObject HNetCfg.FwPolicy2
$firewall.Rules |  Where-Object {$_.Action -eq 0} | 
    Select-Object Name, ApplicationName,LocalPorts

The result may look similar to this:

 
Name           ApplicationName                                         LocalPorts
----           ---------------                                         ----------
pluginhost.exe C:\users\tobwe\appdata\local\skypeplugin\pluginhost.exe *         
pluginhost.exe C:\users\tobwe\appdata\local\skypeplugin\pluginhost.exe *         
spotify.exe    C:\users\tobwe\appdata\roaming\spotify\spotify.exe      *         
spotify.exe    C:\users\tobwe\appdata\roaming\spotify\spotify.exe      *     
 

In Windows 10 and Server 2016, there are finally a number of firewall-specific cmdlets out of the box, too:

 
PS> Get-Command -Noun *Firewall*

CommandType     Name                                               Version    Source                    
-----------     ----                                               -------    ------                    
Function        Copy-NetFirewallRule                               2.0.0.0    NetSecurity               
Function        Disable-NetFirewallRule                            2.0.0.0    NetSecurity               
Function        Enable-NetFirewallRule                             2.0.0.0    NetSecurity               
Function        Get-NetFirewallAddressFilter                       2.0.0.0    NetSecurity               
Function        Get-NetFirewallApplicationFilter                   2.0.0.0    NetSecurity               
Function        Get-NetFirewallInterfaceFilter                     2.0.0.0    NetSecurity               
Function        Get-NetFirewallInterfaceTypeFilter                 2.0.0.0    NetSecurity               
Function        Get-NetFirewallPortFilter                          2.0.0.0    NetSecurity               
Function        Get-NetFirewallProfile                             2.0.0.0    NetSecurity               
Function        Get-NetFirewallRule                                2.0.0.0    NetSecurity               
Function        Get-NetFirewallSecurityFilter                      2.0.0.0    NetSecurity               
Function        Get-NetFirewallServiceFilter                       2.0.0.0    NetSecurity               
Function        Get-NetFirewallSetting                             2.0.0.0    NetSecurity               
Function        New-NetFirewallRule                                2.0.0.0    NetSecurity               
Function        Remove-NetFirewallRule                             2.0.0.0    NetSecurity               
Function        Rename-NetFirewallRule                             2.0.0.0    NetSecurity               
Function        Set-NetFirewallAddressFilter                       2.0.0.0    NetSecurity               
Function        Set-NetFirewallApplicationFilter                   2.0.0.0    NetSecurity               
Function        Set-NetFirewallInterfaceFilter                     2.0.0.0    NetSecurity               
Function        Set-NetFirewallInterfaceTypeFilter                 2.0.0.0    NetSecurity               
Function        Set-NetFirewallPortFilter                          2.0.0.0    NetSecurity               
Function        Set-NetFirewallProfile                             2.0.0.0    NetSecurity               
Function        Set-NetFirewallRule                                2.0.0.0    NetSecurity               
Function        Set-NetFirewallSecurityFilter                      2.0.0.0    NetSecurity               
Function        Set-NetFirewallServiceFilter                       2.0.0.0    NetSecurity               
Function        Set-NetFirewallSetting                             2.0.0.0    NetSecurity               
Function        Show-NetFirewallRule                               2.0.0.0    NetSecurity   
 

Twitter This Tip! ReTweet this Tip!