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.
Get-Command -Module NetSecurity | Measure-ObjectGet-Help Copy-NetFirewallRulePS> 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>] [-NewName217.3 Common parameters and policy stores
| Parameter | Meaning |
|---|---|
| -PolicyStore | Which store: PersistentStore (default), ActiveStore, RSOP, SystemDefaults or a GPO name |
| -GPOSession | A GPO opened with Open-NetGPO |
| -WhatIf / -Confirm | Show what would happen / ask first (WhatIf still reads real rules) |
| -CimSession / -ThrottleLimit / -AsJob | Remote PC / parallel limit / background job |
| -PassThru | Return 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
- Step 1
1.Admin wants the rules of GPO_name inside new_gpo
- Step 2
2.Copy-NetFirewallRule reads the rule and its filters from GPO_name
- Step 3
3.Filters (port, address) are tied 1:1 to the rule, so they come along
- Step 4
4.A complete clone is written into new_gpo (-NewPolicyStore)
- Step 5
5.Same store instead? -NewName is required
# 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
- Step 1
1.Client PC connects to the file server
- Step 2
2.Main mode: main mode rule applies
- Step 3
3.Phase1AuthSet proves identity, MainModeCryptoSet gives key exchange algorithms
- Step 4
4.Quick mode: IPsec rule with Phase2AuthSet and QuickModeCryptoSet
- Step 5
5.Data travels encrypted inside ESP
| Cmdlet | Copies | Own filters |
|---|---|---|
| Copy-NetIPsecMainModeRule | Main mode rule | -MainModeCryptoSet, -Phase1AuthSet, address filter, profile |
| Copy-NetIPsecMainModeCryptoSet | Key exchange algorithms | -ForceDiffieHellman, -MaxMinutes, -MaxSessions |
| Copy-NetIPsecPhase1AuthSet | First authentication | Associated main mode rule or IPsec rule |
| Copy-NetIPsecPhase2AuthSet | Second authentication | Associated IPsec rule only |
| Copy-NetIPsecQuickModeCryptoSet | ESP algorithms | -PerfectForwardSecrecyGroup None|DH1|DH2|DH14|DH19|DH20|DH24|SameAsMainMode |
| Copy-NetIPsecRule | IPsec (quick mode) rule, 44 options | -Mode Tunnel|Transport, -InboundSecurity/-OutboundSecurity, -KeyModule IKEv1|AuthIP|IKEv2 |
$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
- Step 1
1.Server 192.168.10.20 has Allow ping and a test inbound rule Block Ping
- Step 2
2.Client pings: dropped at the firewall (block wins over allow)
- Step 3
3.Admin runs Disable-NetFirewallRule -DisplayName "Block Ping"
- Step 4
4.Rule becomes Enabled False, still in the store
- Step 5
5.Next ping reaches the server and gets a reply; Enable-NetFirewallRule can switch it back on
# 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)
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$ 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# "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$ 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| Windows | nftables |
|---|---|
| Copy-NetFirewallRule -NewName | nft insert rule ... comment <new-name> |
| Disable-NetFirewallRule | nft replace rule ... handle N (same match, no drop verdict) |
| Enable-NetFirewallRule | nft replace rule ... handle N ... drop |
| Remove-NetFirewallRule | nft 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.