PowerShell NetSecurity module (6/7)
Part 6 of the NetSecurity module: the six Rename-NetIPsec cmdlets that give IPsec rules and sets a friendly unique Name, Save-NetGPO that writes a cached GPO back in one go, and the firewall Set- cmdlets that change filters, profiles, Hyper-V settings and whole rules.
222.1 What it does, where it runs
- Built into the NetSecurity module on Windows 10/11 and Windows Server, in PowerShell 5.1 and 7.
- Rename, Set and Save-NetGPO all change firewall/IPsec policy and need Administrator.
- Get-Command for the verbs Rename, Set and Save counts 29 cmdlets.
- On the managed office PC only Get-Command and Get-Help were run; examples come from the Microsoft Learn docs, and the Linux lab shows the same ideas with nftables.
Get-Command -Module NetSecurity ` -Verb Rename,Set,Save | Measure-ObjectGet-Help Set-NetFirewallAddressFilterPS> Get-Command -Module NetSecurity -Verb Rename,Set,Save | Measure-ObjectCount : 29PS> Get-Help Set-NetFirewallAddressFilterremarks : Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying onlypartial help.ModuleName : NetSecuritySynopsis :Set-NetFirewallAddressFilter [-PolicyStore <string>] [-GPOSession <string>] [-LocalAddress<string[]>] [-RemoteAddress <string[]>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>]Set-NetFirewallAddressFilter -InputObject <CimInstance#MSFT_NetAddressFilter[]>[-LocalAddress <string[]>] [-RemoteAddress <string[]>] [-CimSession <CimSession[]>]222.2 Rename-NetIPsec* cmdlets
- An IPsec rule or set created without a Name gets a random value (a long GUID) as its Name.
- Rename gives it a friendly Name; -NewName is required and must be unique in the store.
- When a GPO applies on a client, a local rule with the same Name is overwritten by the GPO rule.
- Rename changes only the Name (ID); the visible DisplayName needs Set-NetIPsecRule -NewDisplayName.
- Step 1
1.IPsec rule created without -Name: Name {ed8384a9-...}
- Step 2
2.Admin PowerShell runs Rename-NetIPsecRule
- Step 3
3.Request goes to the GPO (domain policy store)
- Step 4
4.Name becomes SecureNet - Exempt Servers
- Step 5
5.GPO applies on client PC 192.168.10.10
- Step 6
6.Same Name locally: the GPO rule wins
# Example (docs), admin only, NOT run here:Rename-NetIPsecRule ` -IPsecRuleName ` "{ed8384a9-a78b-4d0d-8f3d-eb5615edb4a0}" ` -NewName "SecureNet - Exempt Servers"Rename-NetIPsecMainModeCryptoSet ` -Name ` "{ed8384a9-a78b-4d0d-8f3d-eb5615edb4a0}" ` -NewName ` "(DA Client) - Phase 2 Crypto Set"Rename-NetIPsecMainModeRule ` -Name ` "{ed8384a9-a78b-4d0d-8f3d-eb5615edb4a0}" ` -NewName ` "Tunnel Mode - Americas (DA Client)"| Cmdlet | Options | Extra selectors |
|---|---|---|
| Rename-NetIPsecRule | 39 | -IPsecRuleName, -Mode, -InboundSecurity/-OutboundSecurity, -KeyModule, auth sets, QM crypto set, -Machine/-User, associated filters |
| Rename-NetIPsecMainModeRule | 22 | -Enabled, -MainModeCryptoSet, -Phase1AuthSet, associated address filter/profile |
| Rename-NetIPsecMainModeCryptoSet | 19 | -ForceDiffieHellman, -MaxMinutes, -MaxSessions, -AssociatedNetIPsecMainModeRule |
| Rename-NetIPsecPhase1AuthSet | 17 | -AssociatedNetIPsecMainModeRule, -AssociatedNetIPsecRule |
| Rename-NetIPsecQuickModeCryptoSet | 17 | -PerfectForwardSecrecyGroup (None, DH1, DH2, DH14, DH19, DH20, DH24, SameAsMainMode) |
| Rename-NetIPsecPhase2AuthSet | 16 | -AssociatedNetIPsecRule only |
- Shared by all six: -Name (Rename-NetIPsecRule uses -IPsecRuleName), -NewName, -DisplayName, -All, -Description, -DisplayGroup/-Group, -InputObject, -PassThru.
- Also shared: -PolicyStoreSource, -PolicyStoreSourceType (None, Local, GroupPolicy, Dynamic, Generated, Hardcoded), -PrimaryStatus, -Status, -TracePolicyStore.
- Docs: giving a Main Mode crypto set a special GUID as its Name makes it the default Main Mode crypto set.
222.3 Save-NetGPO (1 option)
- On a busy domain controller every small change loads and saves the whole GPO; Open-NetGPO brings a cached copy into your session instead.
- Every change is made against the copy with -GPOSession.
- Save-NetGPO -GPOSession writes the whole GPO back in one write.
- Docs warning: if another admin changed the same GPO in between, that change is overwritten on save.
- Step 1
1.Admin: Open-NetGPO -PolicyStore castle.contoso.com\Win8ClientFirewallPolicy
- Step 2
2.Cached copy stored in $gpoSession
- Step 3
3.Remove + New rule with -GPOSession (only in the cache)
- Step 4
4.Second admin changes the same GPO on the DC
- Step 5
5.Save-NetGPO writes the whole GPO at once
- Step 6
6.Second admin's change is lost
If PowerShell closes before Save-NetGPO, nothing changes in the GPO.
# Example (docs), admin only, NOT run here:$gpoSession = Open-NetGPO -PolicyStore ` castle.contoso.com\Win8ClientFirewallPolicyRemove-NetFirewallRule -Name BlockIMAccess ` -GPOSession $gpoSessionNew-NetFirewallRule -Name LimitIMAccess ` -DisplayName "Contoso Messenger" ` -Program `"%ProgramFiles(X86)%\Contoso Messenger\cmsg.exe" ` -Action Block -GPOSession $gpoSessionSave-NetGPO -GPOSession $gpoSession222.4 Set- filter cmdlets
- A firewall rule's conditions (address, port, program, interface) live in separate filter objects.
- Set-NetFirewall*Filter changes one filter and leaves the rule's other filters as they are.
- -InputObject is required: get the filters with Get-NetFirewall*Filter and pipe them in.
- Step 1
1.Rule AllowWeb80 on the server: RemoteAddress 192.168.0.5
- Step 2
2.Client 192.168.0.2 sends HTTP to TCP 80
- Step 3
3.Windows Firewall: no match, dropped
- Step 4
4.Admin sets RemoteAddress of the address filter to 192.168.0.2
- Step 5
5.Next HTTP request reaches the web server
- Step 6
6.Other filters of the rule stay unchanged
# Example (docs), admin only, NOT run here:Set-NetIPsecRule -DisplayName "Tunnel Rule" ` -LocalAddress Any$nfwAddressFilter = ( Get-NetIPsecRule ` -DisplayName "Tunnel Rule" | Get-NetFirewallAddressFilter )Set-NetFirewallAddressFilter ` -InputObject $nfwAddressFilter ` -LocalAddress AnyGet-NetIPsecRule -DisplayName "Tunnel Rule" | Get-NetFirewallAddressFilter | Set-NetFirewallAddressFilter ` -LocalAddress Any| Cmdlet | Options | Key settings | Docs example |
|---|---|---|---|
| Set-NetFirewallAddressFilter | 6 | -LocalAddress, -RemoteAddress (IP, subnet, range or keyword like Any) | Core Networking rules to -RemoteAddress LocalSubnet4 |
| Set-NetFirewallApplicationFilter | 6 | -Program (path), -Package (Store app SID) | -Program %SystemRoot%\System32\messenger.exe |
| Set-NetFirewallInterfaceFilter | 5 | -InterfaceAlias (Any or alias) | Move Wired2 rules to Wired3 |
| Set-NetFirewallInterfaceTypeFilter | 6 | -InterfaceType Any, Wired, Wireless, RemoteAccess | All Wired rules to Any |
| Set-NetFirewallPortFilter | 10 | -Protocol, -LocalPort/-RemotePort, -IcmpType, -DynamicTarget | Rules on port 10246 to -LocalPort Any |
Set-NetFirewallInterfaceFilter ` -InterfaceAlias Wired3 ` -InputObject $nfwInterfaceFilterWired2Get-NetFirewallInterfaceTypeFilter ` -InterfaceType Wired | Set-NetFirewallInterfaceTypeFilter ` -InterfaceType AnyGet-NetFirewallPortFilter | Where-Object -FilterScript ` { $_.LocalPort -Eq "10246" } | Set-NetFirewallPortFilter -LocalPort Any222.5 Hyper-V firewall and profiles
- The Hyper-V firewall is a separate firewall for VM traffic, keyed by a VM creator ID.
- Set-NetFirewallHyperVProfile (6): -Name '{GUID}', -Profile, -Enabled, default inbound/outbound action, -AllowLocalFirewallRules.
- Set-NetFirewallHyperVRule (15): selects only by -Name/-DisplayName (pipe from Get-NetFirewallHyperVRule to search by property); sets -Action, -Direction, ports, addresses, -RulePriority, -Profiles.
- Set-NetFirewallHyperVVMSetting (7): per VM creator defaults, -LoopbackEnabled (guest-host loopback), -AllowHostPolicyMerge.
- Set-NetFirewallProfile (26): Domain/Private/Public settings: -Enabled, default actions, -AllowInboundRules, -NotifyOnListen, logging (-LogFileName, -LogAllowed, -LogBlocked, -LogIgnored, -LogMaxSizeKilobytes).
- Docs: NotConfigured is valid only while configuring a GPO.
Set-NetFirewallHyperVProfile ` -Name ` '{9E288F02-CE00-4D9E-BE2B-14CE463B0298}' ` -Profile Public -Enabled TrueSet-NetFirewallHyperVVMSetting ` -Name ` '9E288F02-CE00-4D9E-BE2B-14CE463B0298}' ` -LoopbackEnabled TrueSet-NetFirewallProfile ` -Profile Domain,Public,Private -Enabled TrueSet-NetFirewallProfile -Name Domain ` -DefaultInboundAction BlockGet-NetFirewallProfile -Name Private ` -PolicyStore corp.contoso.com\gpo_name | Set-NetFirewallProfile ` -AllowUnicastResponseToMulticast True222.6 Set-NetFirewallRule (39 options)
- Changes any property of an existing rule; with -DisplayGroup every rule of the group gets the same change.
- Rule properties: -NewDisplayName, -Enabled, -Action, -Direction, -Profile, -EdgeTraversalPolicy, -OverrideBlockRules.
- Conditions (they go into the filter objects): -LocalAddress/-RemoteAddress, -Protocol, -LocalPort/-RemotePort, -Program, -Service, -InterfaceAlias, -InterfaceType.
- Security: -Authentication NotRequired|Required|NoEncap, -Encryption; docs say -Authentication Required needs a separate IPsec rule.
# Example (docs), admin only, NOT run here:Set-NetFirewallRule ` -DisplayName "AllowWeb80" ` -RemoteAddress "192.168.0.2"Set-NetFirewallRule -DisplayGroup ` "Windows Firewall Remote Management" ` -Enabled TrueEnable-NetFirewallRule -DisplayGroup ` "Windows Firewall Remote Management"Set-NetFirewallRule ` -DisplayName "AllowMessenger" ` -Authentication Required -Profile Domain222.7 Linux equivalent: nftables lab
- Lab: server 192.168.242.10 (NET_ADMIN), clients asha 192.168.242.20 and ravi 192.168.242.30.
- Rename idea = nft rename chain; handle and rules stay the same.
- Set address filter idea = nft replace rule handle 2 with a new source address.
- Save-NetGPO idea = save the table to a file and restore it with nft -f.
nft add table ip n222nft add chain ip n222 input \ '{ type filter hook input priority 0; }'nft add rule ip n222 input \ ip saddr 192.168.242.20 \ icmp type echo-request counter drop \ comment block-pingnft rename chain ip n222 input inboundnft replace rule ip n222 inbound handle 2 \ ip saddr 192.168.242.30 \ icmp type echo-request counter drop \ comment block-pingnft -a list table ip n222[asha] $ ping -c 2 -W 1 192.168.242.10 | tail -22 packets transmitted, 0 received, 100% packet loss, time 1006ms[ravi] $ ping -c 2 -W 1 192.168.242.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1023ms$ nft rename chain ip n222 input inbound$ nft -a list table ip n222table ip n222 { # handle 2 chain inbound { # handle 1 type filter hook input priority filter; policy accept; ip saddr 192.168.242.20 icmp type echo-request counter packets 2 bytes 168 drop comment "block-ping" # handle 2 }}(after nft replace ... handle 2)ip saddr 192.168.242.30 icmp type echo-request counter packets 0 bytes 0 drop comment "block-ping" # handle 2[asha] $ ping -c 2 -W 1 192.168.242.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1034ms[ravi] $ ping -c 2 -W 1 192.168.242.10 | tail -22 packets transmitted, 0 received, 100% packet loss, time 1018msnft list ruleset > /tmp/n222-all.nftnft -c -f /tmp/n222-all.nftnft list table ip n222 > /tmp/n222.nftnft flush table ip n222nft -f /tmp/n222.nftnft delete table ip n222$ nft -c -f /tmp/n222-all.nft/tmp/n222-all.nft:3:62-77: Error: unsupported xtables compat expression, use iptables-nft with this ruleset$ nft list table ip n222 > /tmp/n222.nft; cat /tmp/n222.nfttable ip n222 { chain inbound { type filter hook input priority filter; policy accept; ip saddr 192.168.242.30 icmp type echo-request counter packets 2 bytes 168 drop comment "block-ping" }}$ nft flush table ip n222[ravi] $ ping -c 2 -W 1 192.168.242.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1014ms$ nft -f /tmp/n222.nftip saddr 192.168.242.30 icmp type echo-request counter packets 2 bytes 168 drop comment "block-ping" # handle 3[ravi] $ ping -c 2 -W 1 192.168.242.10 | tail -22 packets transmitted, 0 received, 100% packet loss, time 1010ms- The full ruleset file also holds Docker's nat table (managed by iptables-nft), so nft -c rejected it; save only your own table.
- Restored rules get a new handle (3 instead of 2), just like a recreated Windows rule gets a new identity.
222.8 When to use which
| Goal | Use |
|---|---|
| Change the allowed IP of one rule | Set-NetFirewallRule -RemoteAddress |
| Same change on many rules | Get-NetFirewallAddressFilter | Set-NetFirewallAddressFilter |
| Many changes on a busy GPO | Open-NetGPO ... -GPOSession ... Save-NetGPO |
| Readable ID for an IPsec rule/set | Rename-NetIPsec* -NewName |
| Change the visible name | Set-NetIPsecRule / Set-NetFirewallRule -NewDisplayName |
| Turn a profile on or set default Block | Set-NetFirewallProfile |
222.9 Common mistakes
- Expecting Rename-NetIPsecRule to change the on-screen name; it changes only the unique Name.
- Closing PowerShell before Save-NetGPO: the changes lived only in the cached copy, so the GPO is unchanged.
- Two admins editing the same GPO through sessions: the later Save-NetGPO overwrites the other change.
- Using NotConfigured on a local profile; it is valid only for GPO configuration.
222.10 Interview tips
- Change only the allowed IP of a rule: Set-NetFirewallRule -RemoteAddress.
- Many rules: Get-NetFirewallAddressFilter piped into Set-NetFirewallAddressFilter.
- GPO work: do all changes in an Open-NetGPO session and run Save-NetGPO once at the end.
- Rename = unique Name (ID); NewDisplayName = visible name.
- Linux parallels: nft rename chain, nft replace rule handle, nft -f restore.