I'm trying to identify all firewall rules, which are addressing particular port. Here is how I can do it manually:

Get-NetFirewallPortFilter | Where { $_.LocalPort -Eq "RPC" } `
| Format-Table -Property InstanceID

Now I need to know the name of a rule:

Show-NetFirewallRule | Where { $_.InstanceID –Eq “Netlogon-TCP-RPC-In”} `
| Format-Table -Property DisplayName

Then I get:

Netlogon Service Authz (RPC)

I do not want to do it manually
I've tried following script to get the list of names:

$InstanceIDs = Get-NetFirewallPortFilter | Where { $_.LocalPort -Eq "RPC" } `
| Format-Table -Property InstanceID
foreach ($InstanceID in $InstanceIDs)
    Show-NetFirewallRule | Where { $_.InstanceID –Eq $InstanceID} `
    | Format-Table -Property DisplayName

Got nothing at this point.
Figured out that InstanceID within $InstanceIDs is not a string, but failed to convert it.

Any idea on how make it working?


Your issue has something to do with $InstanceID type (try $InstanceID.Gettype() in your script). Next code snippet could help:

$InstanceIDs =  -split $( 
    Get-NetFirewallPortFilter | 
        Where { $_.LocalPort -Eq "RPC" } | 
            Format-Table -Property InstanceID -HideTableHeaders | 

Show-NetFirewallRule | 
    Where { $_.InstanceID –In $InstanceIDs} |
        ForEach-Object { 
            if ($_.Name) {
                "{0} {1} {2}" -f $_.Name, '==', $_.DisplayName

Note that $InstanceIDs is an array of strings in above code snippet; moreover, $_.Name property in Show-NetFirewallRule corresponds to InstanceID one in Get-NetFirewallPortFilter.

  • Thanks a lot. I knew the problem was with type, just did not know how to convert it. Sep 6 '16 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.