BlackHatSamurai
BlackHatSamurai

Reputation: 23483

Delete Firewall Rule Using Powershell on Windows 7

I am working with Firewalls on a Windows 7 box, and I want to DELETE, not disable, existing rules, and I'm wondering how to do this. To disable, I am doing :

function Disable-IncomingFirewallRule($ruleName)
{
    $firewall = New-Object -ComObject hnetcfg.fwpolicy2

    try
    {
        $rule = $firewall.Rules.Item($ruleName)
        $rule.Enabled = $false
        Write-Host "Firewal rule disabled"
    }
    catch
    {
       Write-Host -ForegroundColor Red "Rule does not exist"

    }

But after this, the rule still exists, and when I run this several times, I will have a number of disabled rules, which is why I want to delete them.

Unfortunately using and of the NetSecurity module is not an option, because these only work on Window 8.

I have search high and low for something to show me how to do this, but have come up short. Any help would be greatly appreciated.

Upvotes: 1

Views: 4413

Answers (2)

Dreami
Dreami

Reputation: 321

Sorry, I can't add comments to the last answer at the moment.

Your problem is solved, but there might be people with other conditions:

Beware that the full advfirewall commands are only available at NT6.0 and higher (Vista and upwards). If you have older clients, the syntax is:

netsh firewall delete allowedprogram "program name"

(That is for deleting program rules, I haven't used it in other ways)

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200213

Use netsh:

netsh advfirewall firewall delete rule name="$ruleName"

Upvotes: 3

Related Questions