How to display firewall rule ports numbers with PowerShell

Displaying firewall rules with PowerShell is very easy with the Get-NetFirewallRule cmdlet.
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,
@{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}},

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

More about

properties (Microsoft Docs)

4 thoughts on “How to display firewall rule ports numbers with PowerShell

  1. Luc, thank you so much for this post. I’ve been trying various method to get a good firewall report for a current project. This bit a code is by far the best I’ve seen, elegant in its simplicity – Keith Risinger

    Liked by 1 person

Leave a Reply

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

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

Facebook photo

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

Connecting to %s