SkillPilotAI

PowerShell NetSecurity module (5/7)

Part 5 of the NetSecurity module covers the Remove cmdlets that permanently delete firewall rules, IPsec rules, crypto/auth sets and live SAs, plus the Rename cmdlets. They need admin and delete policy, so knowing exactly what each one targets is what keeps you from wiping a firewall by accident.

221.1 What it does, where it runs

  • Built in on Windows 10, Windows 11 and Windows Server; works in Windows PowerShell 5.1 and PowerShell 7.
  • Remove = permanent delete. Disable = rule becomes inactive but stays, so it can be enabled again later.
  • All Remove/Rename cmdlets need an elevated (admin) session and change firewall/IPsec policy or live SAs.
  • Get-Command -Module NetSecurity -Verb Remove counts 14 Remove cmdlets in this module.
  • In the video only Get-Command and Get-Help ran on the (managed, non-admin) office PC; everything else is from the Microsoft Learn docs.
PowerShell
Get-Command -Module NetSecurity -Verb Remove |  Measure-ObjectGet-Help Remove-NetFirewallRule
Output
$ Get-Command -Module NetSecurity -Verb Remove | Measure-ObjectCount    : 14 $ Get-Help Remove-NetFirewallRule   (trimmed)Synopsis :Remove-NetFirewallRule [-All] [-PolicyStore <string>] [-GPOSession <string>][-TracePolicyStore] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob] [-PassThru][-WhatIf] [-Confirm] [<CommonParameters>] Remove-NetFirewallRule [-Name] <string[]> [-PolicyStore <string>] [-GPOSession <string>]...Remove-NetFirewallRule -DisplayName <string[]> [-PolicyStore <string>] [-GPOSession...remarks                  : Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying onlypartial help.

221.2 How it works: deleting an Allow rule

Allow rule deleted = default Block
  1. 1.Client 192.168.1.20 sends a request on TCP 8080

  2. 2.Windows Firewall on the server: rule Allow Web 8080 matches

  3. 3.Packet reaches the server 192.168.1.10

  4. 4.Admin runs Remove-NetFirewallRule on that Allow rule

  5. 5.Next request on TCP 8080: no rule matches

  6. 6.Profile default inbound action Block applies, packet dropped at the firewall

Removing an Allow rule does not 'open' anything: traffic falls back to the profile default (Inbound Block).

221.3 Remove-NetFirewallRule (32 options)

  • With NO parameter it deletes ALL static local firewall rules (docs use it to clear conflicts with a domain GPO).
  • Pick rules with -Name (unique ID, default), -DisplayName or -All.
  • Or match on properties: -Enabled, -Direction, -Action, -Group, -Owner and others.
  • Associated filter options (-AssociatedNetFirewall*Filter, 7 of them) find rules by port, address or application filter; -InputObject takes them from the pipeline.
  • -PolicyStore picks the local store or a GPO (e.g. contoso.com\gpo_name).
PowerShell
Remove-NetFirewallRuleRemove-NetFirewallRule -DisplayName `  "Network Discovery (NB-Name-In)"Remove-NetFirewallRule -Enabled False `  -PolicyStore contoso.com\gpo_name$fwAppFilter =  Get-NetFirewallApplicationFilter -Program ` "C:\Program Files (x86)\Messenger\msmsgs.exe"Remove-NetFirewallRule `  -InputObject $fwAppFilter
Option groupOptionsPurpose
Select-Name, -DisplayName, -AllWhich rule(s): unique Name, visible name, or everything in the store
Match-Enabled, -Direction, -Action, -DisplayGroup/-Group, -Description, -Owner, -Status, -PrimaryStatusDelete rules whose properties match
Advanced match-EdgeTraversalPolicy, -LocalOnlyMapping, -LooseSourceMapping, -PolicyAppIdRarely used rule properties
Filters-AssociatedNetFirewall*Filter (7), *Profile, -InputObjectFind rules via their port/address/app filter or the pipeline
Store-PolicyStore, -GPOSession, -TracePolicyStore, -PolicyStoreSource(Type)Local store or a GPO
Common-PassThru, -WhatIf, -Confirm, -AsJobPreview, confirm, run as a job

221.4 Rename-NetFirewallRule (30) + Rename-NetFirewallHyperVRule (11)

  • Changes the Name of a rule, which is its unique ID, not the name shown in the console.
  • A rule created without -Name gets a random GUID; the docs example renames that GUID to a readable name.
  • -NewName must be unique in the store, so only one rule can be renamed at a time.
  • Rename-NetFirewallHyperVRule does the same for Hyper-V firewall rules; extra filters -VMCreatorId, -Protocol, -EnforcementStatus.
PowerShell
Rename-NetFirewallRule -Name `  "{ed8384a9-a78b-4d0d-8f3d-eb5615edb4a0}" `  -NewName `  "Contoso-NETDIS-UPnPHost-Out-TCP-Active"Rename-NetFirewallHyperVRule -Name `  "{ed8384a9-a78b-4d0d-8f3d-eb5615edb4a0}" `  -NewName "Contoso-Out-TCP-Block"

221.5 IPsec cleanup: rules, sets and SAs

IPsec negotiation and what each Remove deletes
  1. 1.Main Mode (IKE): Phase 1 auth set + Main Mode crypto set

  2. 2.Main Mode SA created with peer 192.168.1.10

  3. 3.Quick Mode: Phase 2 auth set + Quick Mode crypto set

  4. 4.Quick Mode SA created, data flows as encrypted ESP

  5. 5.Remove-NetIPsecQuickModeSA deletes the live SA

  6. 6.Next traffic negotiates a new Quick Mode SA

Sets and rules are policy; SAs are live state that gets renegotiated.

CmdletOptionsDeletes
Remove-NetIPsecRule41IPsec rules (no parameter = all static local IPsec policies)
Remove-NetIPsecMainModeRule24Main Mode rules
Remove-NetIPsecMainModeCryptoSet21Main Mode crypto sets (can match -ForceDiffieHellman, -MaxMinutes, -MaxSessions)
Remove-NetIPsecPhase1AuthSet18Phase 1 auth sets
Remove-NetIPsecPhase2AuthSet17Phase 2 auth sets
Remove-NetIPsecQuickModeCryptoSet18Quick Mode crypto sets
Remove-NetIPsecDospSetting6IPsec DoS protection config (those interfaces lose DoS protection)
Remove-NetIPsecMainModeSA7Live Main Mode SAs
Remove-NetIPsecQuickModeSA7Live Quick Mode SAs
PowerShell
Remove-NetIPsecRuleRemove-NetIPsecRule -DisplayName `  " Transport Mode - CorpNet IPv4 - AuthIP"Remove-NetIPsecRule -Enabled False `  -PolicyStore contoso.com\gpo_name$nfProfile = Get-NetFirewallProfile `  -Profile PublicRemove-NetIPsecRule -InputObject $nfProfile Remove-NetIPsecMainModeRule -DisplayName `  "Main Mode Authenticate Computer"Remove-NetIPsecMainModeCryptoSet `  -DisplayName "MM Encryption"Remove-NetIPsecMainModeCryptoSet `  -ForceDiffieHellman $True Remove-NetIPsecQuickModeCryptoSet `  -DisplayName `  "DirectAccess - Phase2 Crypto Set"$ipsRule = Get-NetIPsecRule -DisplayName `  "Transport Mode - Extranet IPv4"Remove-NetIPsecPhase2AuthSet `  -InputObject $ipsRule
  • -ForceDiffieHellman $True deletes every Main Mode crypto set that requires the Diffie-Hellman key exchange.
  • The pipeline example takes a rule with Get-NetIPsecRule and deletes its associated Phase 2 auth sets.
  • -All, -Name, -DisplayName and -Group work the same on every set cmdlet.

221.6 Live SAs and DoS protection

PowerShell
Remove-NetIPsecDospSetting `  -Name "Dosp-IKEv2-CorpNet-PubNet"Get-NetIPsecDospSetting |  Remove-NetIPsecDospSettingRemove-NetIPsecMainModeSA `  -PolicyStore ActiveStoreRemove-NetIPsecQuickModeSA `  -PolicyStore ActiveStore$computer1 = "RemoteMachineName"$ipsMMSA = Get-NetIPsecMainModeSA `  -Name "196511" -CimSession $computer1Remove-NetIPsecQuickModeSA `  -CimSession $computer1 -InputObject $ipsMMSA
  • SA cmdlets do not touch policy, they clear live SAs; -PolicyStore ActiveStore targets the active SAs.
  • -CimSession runs it on a remote computer (RemoteMachineName in the docs).
  • After an SA is deleted the connection negotiates a new one, which makes it a troubleshooting tool.

221.7 Linux equivalent: nftables

  • Lab: Docker network n221-lan 192.168.241.0/24, client 192.168.241.20, server 192.168.241.10 with NET_ADMIN.
  • nft -a shows each rule's handle, which plays the role of the unique Name.
  • Mapping: nft replace rule = Rename, nft delete rule handle N = Remove -Name, nft flush chain = Remove -All, nft delete table = delete everything.
Terminal
nft add table ip n221nft add chain ip n221 input \  '{ type filter hook input priority 0; }'nft add rule ip n221 input \  ip saddr 192.168.241.20 icmp type \  echo-request counter drop comment block-pingnft add rule ip n221 input \  ip saddr 192.168.241.20 tcp dport 8080 \  counter drop comment block-webnft add rule ip n221 input tcp dport 9090 \  counter drop comment block-9090nft -a list table ip n221
Output
$ nft -a list table ip n221table ip n221 { # handle 2  chain input { # handle 1    type filter hook input priority filter; policy accept;    ip saddr 192.168.241.20 icmp type echo-request counter packets 0 bytes 0 drop comment "block-ping" # handle 2    ip saddr 192.168.241.20 tcp dport 8080 counter packets 0 bytes 0 drop comment "block-web" # handle 3    tcp dport 9090 counter packets 0 bytes 0 drop comment "block-9090" # handle 4  }}[client] $ ping -c 2 -W 1 192.168.241.10 | tail -22 packets transmitted, 0 received, 100% packet loss, time 1020ms[client] $ nc -zv -w 2 192.168.241.10 8080nc: connect to 192.168.241.10 port 8080 (tcp) timed out: Operation in progress
Terminal
nft replace rule ip n221 input handle 3 \  ip saddr 192.168.241.20 tcp dport 8080 \  counter drop comment web-8080-blocknft delete rule ip n221 input handle 2nft -a list chain ip n221 inputnft flush chain ip n221 inputnft delete table ip n221nft list tables
Output
$ nft delete rule ip n221 input handle 2$ nft -a list chain ip n221 inputtable ip n221 {  chain input { # handle 1    type filter hook input priority filter; policy accept;    ip saddr 192.168.241.20 tcp dport 8080 counter packets 0 bytes 0 drop comment "web-8080-block" # handle 3    tcp dport 9090 counter packets 0 bytes 0 drop comment "block-9090" # handle 4  }}[client] $ ping -c 2 -W 1 192.168.241.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1028ms[client] $ nc -zv -w 2 192.168.241.10 8080nc: connect to 192.168.241.10 port 8080 (tcp) timed out: Operation in progress $ nft flush chain ip n221 input[client] $ nc -zv -w 2 192.168.241.10 8080Connection to 192.168.241.10 8080 port [tcp/http-alt] succeeded!$ nft delete table ip n221$ nft list tablestable ip nat

221.8 Comparison: Remove vs Disable vs Rename

PointRemove-NetFirewallRuleDisable-NetFirewallRuleRename-NetFirewallRule
EffectRule deleted permanentlyRule stays, inactiveOnly the unique Name changes
UndoRecreate it from scratchEnable-NetFirewallRuleRename again
TrafficFalls to the profile defaultFalls to the profile defaultNo change
Linux nftdelete rule handle N(delete or recreate later)replace rule (handle stays)

221.9 Common mistakes

  • Running Remove-NetFirewallRule with no parameter in an admin shell: per the docs it deletes ALL static local rules.
  • Believing Rename changes the console name: it changes only Name; use Set-NetFirewallRule -NewDisplayName for the display name.
  • Using Remove when you may need the rule back; Disable keeps it.
  • Forgetting -PolicyStore when the rule lives in a GPO, so nothing is removed from the GPO.

221.10 Interview tips

  • Safe delete: first Get-NetFirewallRule with the same filter, then target only that rule with -Name/-DisplayName, add -WhatIf or -Confirm.
  • Need it back later? Disable-NetFirewallRule, not Remove.
  • IPsec cleanup: rules, crypto sets and auth sets each have their own Remove cmdlet; live SAs are cleared with Remove-NetIPsecQuickModeSA/MainModeSA.
  • Everything needs admin; for a GPO pass -PolicyStore.
  • Linux equivalent: nft delete rule handle, nft flush chain, nft delete table.