2

I have been trying to get only specific data from the firewall rules in powershell. The command i tried is Get-NetFirewallRule | select DisplayName, Enabled, but it does not output required one.

Kartik
  • 23
  • 3
  • I talk about the firewall filter commands here: https://stackoverflow.com/questions/42110526/why-doesnt-get-netfirewallrule-show-all-information-of-the-firewall-rule/58138487#58138487 – js2010 Oct 17 '20 at 15:47

1 Answers1

1

Try like this (It's a bit slow):

$rules = Get-NetFirewallRule
$rules | Foreach {
    $rule = (Get-NetFirewallRule -DisplayName $_.DisplayName | Get-NetFirewallPortFilter)
    If($rule.Protocol -eq "TCP"){
      [PSCustomObject]@{
        'Rule Name' = $_.DisplayName
        'TCP Port' = $rule.LocalPort
        'Enabled' = $_.Enabled
      }
  }
}
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Thank you. II have a small question , Is there any direct command without declaring variable? Like in this case $rules has been assigned. For the required output is it necessary to assign a variable first and do the rest? – Kartik Oct 17 '20 at 12:34
  • No, you can do `(Get-NetFirewallRule)` in place of `$rules` inside Foreach loop – Wasif Oct 17 '20 at 12:41
  • Alright.. Thank You. Helped a lot!! – Kartik Oct 17 '20 at 12:53
  • If this helped then please click on the big tick mark to accept it as answer of this question. Welcome – Wasif Oct 17 '20 at 12:54