SkillPilotAI

PowerShell NetSecurity module (1/7)

The NetSecurity module manages Windows Firewall and IPsec from PowerShell as objects, the same job netsh advfirewall does. Part 1 covers the Copy- cmdlets (clone firewall rules and IPsec rules/sets between policy stores and GPOs) and the Disable- cmdlets (switch a rule off without deleting it).

217.1 Why is it used?

  • Clone a firewall rule (with its port/address filters) inside a store or into another GPO.
  • Copy complete IPsec main mode / quick mode setups (rules + auth sets + crypto sets) to a test GPO.
  • Switch rules off temporarily for troubleshooting; they stay in the store and can be enabled again.
  • Disable Hyper-V firewall rules separately from normal rules.

217.2 Where it runs

  • Built into Windows 10/11 and Server, PowerShell 5.1 and 7; 106 commands in the module.
  • Copy- and Disable- change firewall/IPsec policy and need Administrator.
  • In the video the office PC (company-managed firewall) only ran Get-Command and Get-Help; examples are from Microsoft Learn; Linux equivalent is nftables in Docker.
PowerShell
Get-Command -Module NetSecurity | Measure-ObjectGet-Help Copy-NetFirewallRule
Output
PS> Get-Command -Module NetSecurity | Measure-ObjectCount    : 106PS> Get-Help Copy-NetFirewallRuleremarks                  : Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying onlypartial help.ModuleName               : NetSecuritySynopsis                 :Copy-NetFirewallRule [-All] [-PolicyStore <string>] [-GPOSession <string>]Copy-NetFirewallRule [-Name] <string[]> [-PolicyStore <string>] [-GPOSession <string>]Copy-NetFirewallRule -DisplayName <string[]> [-PolicyStore <string>] [-GPOSession <string>][-TracePolicyStore] [-NewPolicyStore <string>] [-NewGPOSession <string>] [-NewName

217.3 Common parameters and policy stores

ParameterMeaning
-PolicyStoreWhich store: PersistentStore (default), ActiveStore, RSOP, SystemDefaults or a GPO name
-GPOSessionA GPO opened with Open-NetGPO
-WhatIf / -ConfirmShow what would happen / ask first (WhatIf still reads real rules)
-CimSession / -ThrottleLimit / -AsJobRemote PC / parallel limit / background job
-PassThruReturn the resulting object
  • A policy store is a container of firewall and IPsec rules: this PC's PersistentStore or a domain GPO.
  • One Name belongs to only one rule in a store, so copying inside the same store needs -NewName.

217.4 Copy-NetFirewallRule

Policy store to policy store: Copy-NetFirewallRule
  1. 1.Admin wants the rules of GPO_name inside new_gpo

  2. 2.Copy-NetFirewallRule reads the rule and its filters from GPO_name

  3. 3.Filters (port, address) are tied 1:1 to the rule, so they come along

  4. 4.A complete clone is written into new_gpo (-NewPolicyStore)

  5. 5.Same store instead? -NewName is required

PowerShell
# Example (docs), admin only, NOT run here:Copy-NetFirewallRule -DisplayName `  "Require Outbound Authentication" `  -NewName "Alternate Require Outbound Authentication"Copy-NetFirewallRule -Group "@FirewallAPI.dll,-36501" `  -Enabled $False `  -PolicyStore domain.contoso.com\GPO_name `  -NewPolicyStore domain.contoso.com\new_gpoGet-NetFirewallProfile -Profile Domain `  -PolicyStore domain.contoso.com\GPO_name |  Copy-NetFirewallRule -NewPolicyStore domain.example.com\new_gpo
  • Select rules by -Name (default), -DisplayName, -All, -Group/-DisplayGroup, or filters like -Enabled, -Direction, -Action, -EdgeTraversalPolicy.
  • -AssociatedNetFirewallPortFilter / AddressFilter / ApplicationFilter / Profile find the rule from a filter object.
  • Target: -NewName, -NewPolicyStore, -NewGPOSession; when copying in the same store the DisplayName stays the same.

217.5 Copy-NetIPsec* cmdlets

IPsec objects: which set in which phase
  1. 1.Client PC connects to the file server

  2. 2.Main mode: main mode rule applies

  3. 3.Phase1AuthSet proves identity, MainModeCryptoSet gives key exchange algorithms

  4. 4.Quick mode: IPsec rule with Phase2AuthSet and QuickModeCryptoSet

  5. 5.Data travels encrypted inside ESP

CmdletCopiesOwn filters
Copy-NetIPsecMainModeRuleMain mode rule-MainModeCryptoSet, -Phase1AuthSet, address filter, profile
Copy-NetIPsecMainModeCryptoSetKey exchange algorithms-ForceDiffieHellman, -MaxMinutes, -MaxSessions
Copy-NetIPsecPhase1AuthSetFirst authenticationAssociated main mode rule or IPsec rule
Copy-NetIPsecPhase2AuthSetSecond authenticationAssociated IPsec rule only
Copy-NetIPsecQuickModeCryptoSetESP algorithms-PerfectForwardSecrecyGroup None|DH1|DH2|DH14|DH19|DH20|DH24|SameAsMainMode
Copy-NetIPsecRuleIPsec (quick mode) rule, 44 options-Mode Tunnel|Transport, -InboundSecurity/-OutboundSecurity, -KeyModule IKEv1|AuthIP|IKEv2
PowerShell
$mMrule = Get-NetIPsecMainModeRule `  -DisplayName "Main Mode Rule: P1Auth + Crypto" `  -PolicyStore domain.contoso.com\GPO_name$newGPO = Open-NetGPO -PolicyStore domain.contoso.com\new_GPOCopy-NetIPsecPhase1AuthSet -InputObject $mMrule -GPOSession $newGPOCopy-NetIPsecMainModeCryptoSet -InputObject $mMrule -GPOSession $newGPOCopy-NetIPsecMainModeRule -InputObject $mMrule -GPOSession $newGPOSave-NetGPO -GPOSession $newGPO
  • Open-NetGPO opens the GPO in a session, Save-NetGPO writes it back once (fewer round trips to the domain controller).
  • After the copy you do not re-link the sets: the rule keeps the same set names.
  • Shared options of all six: -Name, -DisplayName, -All, -Group, -InputObject, -NewName, -NewPolicyStore, -NewGPOSession, -PassThru.

217.6 Disable-NetFirewallRule and Disable-NetFirewallHyperVRule

Disable-NetFirewallRule: the rule stays, only off
  1. 1.Server 192.168.10.20 has Allow ping and a test inbound rule Block Ping

  2. 2.Client pings: dropped at the firewall (block wins over allow)

  3. 3.Admin runs Disable-NetFirewallRule -DisplayName "Block Ping"

  4. 4.Rule becomes Enabled False, still in the store

  5. 5.Next ping reaches the server and gets a reply; Enable-NetFirewallRule can switch it back on

PowerShell
# Example (docs), admin only, NOT run here:Disable-NetFirewallRule -DisplayName "Network Discovery"Disable-NetFirewallRule -Group "@FirewallAPI.dll,-28502"Disable-NetFirewallRule -Direction Outbound `  -PolicyStore contoso.com\gpo_nameGet-NetFirewallRule -PolicyStore ActiveStore `  -PolicyStoreSourceType Dynamic |  Disable-NetFirewallRuleDisable-NetFirewallHyperVRule -DisplayName 'MyServerIPBlock'
  • Docs warning: without parameters these cmdlets disable all rules.
  • Selectors are the same as Copy-NetFirewallRule, but there are no target options: the rule is disabled where it is.
  • A rule disabled in a GPO applies on the client at the next GPO refresh.
  • Hyper-V firewall rules are separate; -VMCreatorId says which VM creator a rule belongs to. Disable-NetFirewallHyperVRule -PolicyStore ActiveStore disables all of them, so always filter.

217.7 Linux equivalent: nftables (Docker lab)

Terminal
nft add table ip n217nft add chain ip n217 input \  '{ type filter hook input priority 0; }'nft add rule ip n217 input \  ip saddr 192.168.237.20 \  icmp type echo-request counter drop \  comment block-client-pingnft -a list table ip n217# from the client (192.168.237.20):ping -c 2 -W 1 192.168.237.10 | tail -2
Output
$ nft -a list table ip n217table ip n217 { # handle 2  chain input { # handle 1    type filter hook input priority filter; policy accept;    ip saddr 192.168.237.20 icmp type echo-request counter packets 0 bytes 0 drop comment "block-client-ping" # handle 2  }}[client] $ ping -c 2 -W 1 192.168.237.10 | tail -22 packets transmitted, 0 received, 100% packet loss, time 1029ms
Terminal
# "copy": insert the same match with a new commentnft insert rule ip n217 input position 2 \  ip saddr 192.168.237.20 \  icmp type echo-request counter drop \  comment block-client-ping-copynft delete rule ip n217 input handle 3# "disable": replace with the same match, no dropnft replace rule ip n217 input handle 2 \  ip saddr 192.168.237.20 \  icmp type echo-request counter \  comment block-client-ping-disablednft delete table ip n217
Output
$ nft -a list table ip n217type filter hook input priority filter; policy accept;ip saddr 192.168.237.20 icmp type echo-request counter packets 0 bytes 0 drop comment "block-client-ping-copy" # handle 3ip saddr 192.168.237.20 icmp type echo-request counter packets 2 bytes 168 drop comment "block-client-ping" # handle 2$ nft -a list table ip n217type filter hook input priority filter; policy accept;ip saddr 192.168.237.20 icmp type echo-request counter packets 0 bytes 0 comment "block-client-ping-disabled" # handle 2[client] $ ping -c 2 -W 1 192.168.237.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1007msrtt min/avg/max/mdev = 0.132/0.134/0.136/0.002 ms
Windowsnftables
Copy-NetFirewallRule -NewNamenft insert rule ... comment <new-name>
Disable-NetFirewallRulenft replace rule ... handle N (same match, no drop verdict)
Enable-NetFirewallRulenft replace rule ... handle N ... drop
Remove-NetFirewallRulenft delete rule ... handle N

217.8 Common mistakes

  • Thinking Disable = Remove: a disabled rule still exists and can be enabled; Remove-NetFirewallRule deletes it permanently.
  • Running Disable-NetFirewallRule without a selector: every rule gets disabled.
  • Copying in the same store without -NewName (Name must be unique per store).
  • Expecting a GPO change to apply instantly on clients: it waits for the GPO refresh.

217.9 Interview tips

  • Switch off all outbound rules of a GPO without deleting: Disable-NetFirewallRule -Direction Outbound -PolicyStore <GPO>.
  • GPO firewall troubleshooting: copy rules + auth/crypto sets to a PC without that GPO, disable rules one by one in the local store, then fix only that change in the production GPO.
  • IPsec objects: main mode = Phase1AuthSet + MainModeCryptoSet; quick mode = IPsec rule + Phase2AuthSet + QuickModeCryptoSet.
  • Use Open-NetGPO / Save-NetGPO for batch GPO edits.