So I wanted to get a powershell command to list the firewall rules and ports, and I had to basically use 2 commands: Get-NetFirewallRule
and Get-NetFirewallPortFilter
.
So I basically came up with this:
Get-NetFirewallPortFilter | where-object {$_.LocalPort -cmatch '[0-9]+'}|select-object -Property @{n='Name';e={($_ | Get-NetFirewallRule).Name}}, @{n='Description';e={($_ | Get-NetFirewallRule).description}}, Protocol, LocalPort, RemotePort, @{n='Direction';e={($_|Get-NetFirewallRule).Direction}}
I went with Get-NetFirewallPortFilter
first because I wanted to only get numerical ports (so I didn't want to get 'Any' as a result).
Problem is, the more calculated properties I have, the slower this runs. It generates like 3 lines a second, which IMO is very slow.
I timed the command to see how long it takes (did an Out-GridView
to also see the output, I don't think it adds THAT much more to the time, it seems to generate just as many lines a second):
PS C:\> measure-command { Get-NetFirewallPortFilter | where-object {$_.LocalPort -cmatch '[0-9]+'}|select-object -Property @{n='Name';e={($_ | Get-NetFirewallRule).Name}}, @{n='Description';e={($_ | Get-NetFirewallRule).description}}, Protocol, LocalPort, RemotePort, @{n='Direction';e={($_|Get-NetFirewallRule).Direction}} |out-gridview }
Days : 0
Hours : 0
Minutes : 1
Seconds : 59
Milliseconds : 143
Ticks : 1191434968
TotalDays : 0.00137897565740741
TotalHours : 0.0330954157777778
TotalMinutes : 1.98572494666667
TotalSeconds : 119.1434968
TotalMilliseconds : 119143.4968
It output 358 lines in ~119 seconds.
Are there any optimizations I can run to make this run faster?