Edit firewall rule scope with PowerShell

One of our managed server customer frequently asked me to add an IP address to the scope of a firewall. Specially when they were still testing and did not want HTTP/HTTPS to be open for everyone.

Recent versions of PowerShell have Cmdlets that you can use to manage firewall rules. To set the scope you can use Set-NetFirewallAddressFilter Cmdlet. You use it like this.

Get-NetFirewallrule -DisplayName 'Test-Rule' |
  Get-NetFirewallAddressFilter |
  Set-NetFirewallAddressFilter -RemoteAddress 192.168.5.5

This works well and fast. The only problem is that it also overwrites everything that is already in the RemoteAddress list. To add an IP address you need to get the current value first, then add the new IP address to that value and finally set the new scope.

To make my life more easier I created a function to do all this. It is essentially a wrapper around Set-NetFirewallAddressFilter. And you can use it the same way.

Get-NetFirewallrule -DisplayName 'Test-Rule' |
  Get-NetFirewallAddressFilter |
  Add-MvaNetFirewallRemoteAdressFilter -IPAddresses 192.168.5.5