How to display firewall rule ports numbers with PowerShell

Displaying firewall rules with PowerShell is very easy with the Get-NetFirewallRule cmdlet.
However, there is a gap: port numbers are not displayed.

Here is what you get with the default view:

There is a cmdlet named Get-NetFirewallPortFilter but it displays only information related to ports and you don’t know to which rule it is associated.

And you get the same result when you pipe rules to the Get-NetFirewallPortFilter cmdlet.

The solution

To display everything in a single output you can use calculated properties. They can be used with several cmdlets like the Select-Object or the Format-Table cmdlets.

Here is an example in which I display only information for the Remote Desktop group.

 Get-NetFirewallRule -DisplayGroup 'Remote Desktop' |
Format-Table -Property Name,
DisplayName,
DisplayGroup,
@{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 

And as you can notice, the output displays now ports as well.

More about

Calculated properties (Microsoft Docs)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s