SkillPilotAI

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.
PowerShell
Get-Command -Module NetSecurity `  -Verb Rename,Set,Save | Measure-ObjectGet-Help Set-NetFirewallAddressFilter
Output
PS> 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.
Name = unique ID: Rename-NetIPsecRule
  1. 1.IPsec rule created without -Name: Name {ed8384a9-...}

  2. 2.Admin PowerShell runs Rename-NetIPsecRule

  3. 3.Request goes to the GPO (domain policy store)

  4. 4.Name becomes SecureNet - Exempt Servers

  5. 5.GPO applies on client PC 192.168.10.10

  6. 6.Same Name locally: the GPO rule wins

PowerShell
# 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)"
CmdletOptionsExtra selectors
Rename-NetIPsecRule39-IPsecRuleName, -Mode, -InboundSecurity/-OutboundSecurity, -KeyModule, auth sets, QM crypto set, -Machine/-User, associated filters
Rename-NetIPsecMainModeRule22-Enabled, -MainModeCryptoSet, -Phase1AuthSet, associated address filter/profile
Rename-NetIPsecMainModeCryptoSet19-ForceDiffieHellman, -MaxMinutes, -MaxSessions, -AssociatedNetIPsecMainModeRule
Rename-NetIPsecPhase1AuthSet17-AssociatedNetIPsecMainModeRule, -AssociatedNetIPsecRule
Rename-NetIPsecQuickModeCryptoSet17-PerfectForwardSecrecyGroup (None, DH1, DH2, DH14, DH19, DH20, DH24, SameAsMainMode)
Rename-NetIPsecPhase2AuthSet16-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.
Open-NetGPO, cached copy, Save-NetGPO
  1. 1.Admin: Open-NetGPO -PolicyStore castle.contoso.com\Win8ClientFirewallPolicy

  2. 2.Cached copy stored in $gpoSession

  3. 3.Remove + New rule with -GPOSession (only in the cache)

  4. 4.Second admin changes the same GPO on the DC

  5. 5.Save-NetGPO writes the whole GPO at once

  6. 6.Second admin's change is lost

If PowerShell closes before Save-NetGPO, nothing changes in the GPO.

PowerShell
# 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 $gpoSession

222.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.
Address filter change: which remote is allowed
  1. 1.Rule AllowWeb80 on the server: RemoteAddress 192.168.0.5

  2. 2.Client 192.168.0.2 sends HTTP to TCP 80

  3. 3.Windows Firewall: no match, dropped

  4. 4.Admin sets RemoteAddress of the address filter to 192.168.0.2

  5. 5.Next HTTP request reaches the web server

  6. 6.Other filters of the rule stay unchanged

PowerShell
# 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
CmdletOptionsKey settingsDocs example
Set-NetFirewallAddressFilter6-LocalAddress, -RemoteAddress (IP, subnet, range or keyword like Any)Core Networking rules to -RemoteAddress LocalSubnet4
Set-NetFirewallApplicationFilter6-Program (path), -Package (Store app SID)-Program %SystemRoot%\System32\messenger.exe
Set-NetFirewallInterfaceFilter5-InterfaceAlias (Any or alias)Move Wired2 rules to Wired3
Set-NetFirewallInterfaceTypeFilter6-InterfaceType Any, Wired, Wireless, RemoteAccessAll Wired rules to Any
Set-NetFirewallPortFilter10-Protocol, -LocalPort/-RemotePort, -IcmpType, -DynamicTargetRules on port 10246 to -LocalPort Any
PowerShell
Set-NetFirewallInterfaceFilter `  -InterfaceAlias Wired3 `  -InputObject $nfwInterfaceFilterWired2Get-NetFirewallInterfaceTypeFilter `  -InterfaceType Wired |  Set-NetFirewallInterfaceTypeFilter `  -InterfaceType AnyGet-NetFirewallPortFilter |  Where-Object -FilterScript `  { $_.LocalPort -Eq "10246" } |  Set-NetFirewallPortFilter -LocalPort Any

222.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.
PowerShell
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 True

222.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.
PowerShell
# 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 Domain

222.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.
Terminal
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
Output
[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 1018ms
Terminal
nft 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
Output
$ 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

GoalUse
Change the allowed IP of one ruleSet-NetFirewallRule -RemoteAddress
Same change on many rulesGet-NetFirewallAddressFilter | Set-NetFirewallAddressFilter
Many changes on a busy GPOOpen-NetGPO ... -GPOSession ... Save-NetGPO
Readable ID for an IPsec rule/setRename-NetIPsec* -NewName
Change the visible nameSet-NetIPsecRule / Set-NetFirewallRule -NewDisplayName
Turn a profile on or set default BlockSet-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.