I'm trying to create a PS script that creates a Windows firewall rule, blocking the port "6672" for a certain .exe file on an INBOUND connection but lets me have the option to whitelist certain IPs.

The way to do this manually means I have to block the port (6672) for my .exe file completely to ALL IP's then add a range leaving out the IP's I want to whitelist.

For Example if I want to add "" I have to set a range on the rule for "" to "" then another for "" to "". The reason I have to do it this was it that in Windows firewall it looks like you cannot whitelist single IP's from a block rule and block rules override any allow rules.

So back to my original point, I'm really struggling for ideas on how to resolve this at the moment as I can't find a workaround. Is there a way anybody knows of to be able to exclude certain IP's from a rule or how to make an allow rule be prioritised over a block rule?


So i have THIS CODE:

New-NetFirewallRule -DisplayName "GTAO" -Direction Inbound -LocalPort 6672 -Protocol UDP -Action Block
New-NetFirewallRule -DisplayName "GTAO" -Direction Outbound -LocalPort 6672 -Protocol UDP -Action Block

I basically want something like this to override the previous rule creating a "whitelist" on this rule for individual IP's

$UIP = Read-Host "Enter IP to whitelist"
New-NetFirewallRule -DisplayName "GTA$UIP" -Direction Inbound -LocalPort 6672 -Protocol UDP -Action Allow -RemoteAddress $UIP

This is not a PS specific issue, but a 'How do I make WF do X or Y that it was not designed for.'

Very similar to this discussion and suggestions, some of which you have already hinted at.

How to Make One Exception to a Windows Firewall Outbound Rule

How to Make One Exception to a Windows Firewall Outbound Rule

According to the Windows Firewall documentation, block rules always take precedence over allow rules, therefore even if your allow rule looks more specific than a block rule, the allow rule will not work, and the traffic matching both allow and block rules will be blocked. The option “Allow this firewall rule to override block rules” is available only for rules which require IPSec, and is not available for outbound rules.

The only thing you could do with Windows Firewall to achieve something close to what you need is to switch the default behavior for outbound connections to “Block”, then add explicit allow rules for all outbound connections that you need (not just for that single program). Alternatively, you can look for third-party firewall software with more features.

... you can make the above rule for a single program as well, simply set the rule for a program not all programs.

so you set up 2 or more rules (depending on how many ip addresses you want to allow) and block all others. so for example if you only want to allow


you set up rules:

block from to and to and to

  • That actually makes alot of sense. Oh man, now i have to think of how to get the ip ranges as a variable output... Regex here we come! xD – Sam.92 Nov 3 '18 at 20:43

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.