PowerShell NetSecurity module (7/7)
The last part of the NetSecurity module: the remaining Set cmdlets (security/service filters, global firewall settings, IPsec DoS, crypto sets, auth sets and rules), the read-only Show cmdlets, and Sync/Update that keep IPsec rule addresses current. Knowing per-profile vs global settings is a classic interview point.
223.1 What it does, where it runs
- Built into Windows 10/11 and Windows Server; PowerShell 5.1 and 7.
- Set, Sync and Update change firewall/IPsec policy and need an Administrator PowerShell.
- Show-* changes nothing but prints the whole policy, so it is still sensitive on a managed machine.
- The module has 20 Set cmdlets; on the office PC only Get-Command and Get-Help ran, the rest is from Microsoft Learn.
Get-Command -Module NetSecurity -Verb Set | Measure-ObjectGet-Help Set-NetFirewallSetting$ Get-Command -Module NetSecurity -Verb Set | Measure-ObjectCount : 20$ Get-Help Set-NetFirewallSetting (trimmed)Synopsis : Set-NetFirewallSetting [-PolicyStore <string>] [-GPOSession <string>] [-Exemptions <TrafficExemption>] [-EnableStatefulFtp <GpoBoolean>] [-EnableStatefulPptp <GpoBoolean>] [-RemoteMachineTransportAuthorizationList <string>] ... [-CertValidationLevel <CRLCheck>] [-AllowIPsecThroughNAT <IPsecThroughNAT>] [-MaxSAIdleTimeSeconds <uint32>] [-KeyEncoding <KeyEncoding>] [-EnablePacketQueuing <PacketQueuing>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]ModuleName : NetSecurityremarks : Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.223.2 How it works: a global setting
- Step 1
1.Server 192.168.1.10 has an IPsec rule: Require inbound authentication
- Step 2
2.Client 192.168.1.20 pings without IPsec
- Step 3
3.Windows Firewall drops it: not authenticated, client sees Request timed out.
- Step 4
4.Admin: Set-NetFirewallSetting -Exemptions Icmp
- Step 5
5.ICMP is exempt from IPsec requirements (global, every profile)
- Step 6
6.Next ping passes the firewall and reaches the server
The change is in the computer-wide setting, not in any one rule.
223.3 Set-NetFirewallSecurityFilter (10) + Set-NetFirewallServiceFilter (5)
- Security filter = a rule's security conditions: -Authentication, -Encryption, -OverrideBlockRules, -LocalUser, -RemoteUser, -RemoteMachine.
- Service filter links a rule to a Windows service (-Service).
- Both take the filter object from the pipeline.
Get-NetFirewallRule ` -DisplayGroup "*Printer*" | Get-NetFirewallSecurityFilter | Set-NetFirewallSecurityFilter ` -Authentication RequestGet-NetFirewallRule ` -Group "@FirewallAPI.dll,-30502" | Get-NetFirewallServiceFilter -Service Any | Set-NetFirewallServiceFilter ` -Service Ssdpsrv223.4 Set-NetFirewallSetting (17)
Settings that are the same whichever network profile is active.
| Option | Values | Meaning |
|---|---|---|
| -Exemptions | None, NeighborDiscovery, Icmp, RouterDiscovery, Dhcp, NotConfigured | Traffic exempt from IPsec requirements |
| -EnableStatefulFtp / -EnableStatefulPptp | GpoBoolean | How the extra FTP/PPTP connections are handled |
| -CertValidationLevel | None, AttemptCrlCheck, RequireCrlCheck | Certificate revocation list check |
| -AllowIPsecThroughNAT | None, Server, Both | IPsec across NAT |
| -MaxSAIdleTimeSeconds | 300..3600 | After how long an idle SA is deleted |
| -KeyEncoding | UTF16, UTF8 | Preshared key encoding |
| -EnablePacketQueuing | None, Inbound, Forward | Packet queuing |
| Authorization lists | RemoteMachine/RemoteUser Tunnel/Transport lists | Allowed computers and users at the IPsec layer |
Get-NetFirewallSetting ` -PolicyStore corp.contoso.com/gpo_name | Set-NetFirewallSetting ` -Exemptions RouterDiscovery223.5 IPsec Set cmdlets
| Cmdlet | Options | What it changes / docs example |
|---|---|---|
| Set-NetIPsecDospSetting | 27 | IPv6 DoS protection public -> internal; needs at least 1 public + 1 private interface; MaxStateEntries default 75000 |
| Set-NetIPsecMainModeCryptoSet | 15 | Main Mode crypto; DA Client group -MaxMinutes 240 (0..2879) |
| Set-NetIPsecQuickModeCryptoSet | 12 | Quick Mode proposals (AES128 + AESGMAC128), -PerfectForwardSecrecyGroup |
| Set-NetIPsecMainModeRule | 19 | Enable DA Client Main Mode rules; back to Default crypto set via dot notation |
| Set-NetIPsecPhase1AuthSet | 11 | Computer authentication; new root certificate proposal |
| Set-NetIPsecPhase2AuthSet | 11 | User authentication; -NewDisplayName |
| Set-NetIPsecRule | 39 | -Mode, -InboundSecurity/-OutboundSecurity, -KeyModule, ports, addresses, tunnel endpoints, auth sets |
Set-NetIPsecDospSetting -Name PubNet-CorpNet ` -PublicInterfaceAliases PubNet2$dosPSetting = Get-NetIPsecDospSetting ` -Name PubNet-CorpNet$dosPSetting.IpV6IPsecUnauthDscp = "Disabled" Set-NetIPsecMainModeCryptoSet ` -DisplayGroup "DA Client" -MaxMinutes 240$crypto1 = ` New-NetIPsecQuickModeCryptoProposal ` -Encryption AES128 -ESPHash AESGMAC128Set-NetIPsecQuickModeCryptoSet -DisplayName ` "Exchange HIPAA Server, 80 <-> Any ..." ` -Proposals $crypto1,$crypto2Set-NetIPsecMainModeRule ` -DisplayGroup "DA Client" -Enabled True$rule = Get-NetIPsecMainModeRule ` -DisplayName "Tunnel Mode - (DA Client)"$rule.MainModeModeCryptoSet = "Default"Set-NetIPsecMainModeRule -InputObject $ruleSet-NetIPsecPhase2AuthSet -DisplayGroup ` "Authenticate with Certificates" ` -NewDisplayName ` "User Authentication Certificates" $kerbComputer = New-NetIPsecAuthProposal ` -Kerberos -Machine$Phase1AuthSet = New-NetIPsecPhase1AuthSet ` -DisplayName "Computer Kerb Auth" ` -Proposal $kerbComputerSet-NetIPsecRule ` -DisplayName "SecureNet Rule" ` -Phase1AuthSet $Phase1AuthSet.Name- Dot notation pattern: get the object, change a property, then commit it with Set-... -InputObject.
- -InboundSecurity/-OutboundSecurity: None, Request or Require; -Mode: Tunnel or Transport.
- -PerfectForwardSecrecyGroup chooses the Diffie-Hellman group (None, DH1, DH2, DH14, DH19, DH20, DH24, SameAsMainMode).
223.6 Show-, Sync- and Update- cmdlets
- Show-NetFirewallRule / Show-NetIPsecRule: print every rule with its associated objects; -PolicyStore ActiveStore = all stores that apply to the computer.
- Update-NetFirewallDynamicKeywordAddress: updates a dynamic keyword's addresses; -Append $True adds instead of replacing; braces around the -Id GUID are required.
- Sync-NetIPsecRule (42): resolves -Domains/-Servers via -DnsServers and updates rule endpoints; -EndpointType Endpoint1 (local) or Endpoint2 (remote); pass -Confirm.
- Update-NetIPsecRule (8): directly adds/deletes IPv4/IPv6 addresses in one rule; the script from Get-DAPolicyChange uses it.
- Step 1
1.Admin runs Sync-NetIPsecRule on a DirectAccess IPsec rule
- Step 2
2.DNS query for -Domains and -Servers to 192.168.1.53
- Step 3
3.DNS returns the current IPs
- Step 4
4.Cmdlet compares them with the rule's existing addresses
- Step 5
5.Delta computed: Add / Delete
- Step 6
6.Rule endpoints updated in the GPO (server_GPO)
Show-NetFirewallRule -PolicyStore ActiveStoreShow-NetIPsecRule -PolicyStore ActiveStoreUpdate-NetFirewallDynamicKeywordAddress ` -Id ` "{01234567-89ab-cdef-0123-456789abcdef}" ` -Addresses 192.0.0.1 -Append $TrueSync-NetIPsecRule ` -PolicyStore $serverPolicyStore ` -IPsecRuleName $serverRuleName ` -EndpointType endpoint1 ` -Domains $domains -Servers $servers ` -DnsServers $primaryDns64 ` -AddressType IPv6 -ConfirmUpdate-NetIPsecRule ` -IPsecRuleName "IPsec Rule" -Action Add ` -IPv4addresses $IPv4list ` -IPv6addresses $IPv6list ` -EndpointType Endpoint2 ` -PolicyStore domain.contoso.com/sample_gpo223.7 Linux equivalent: chain policy and nft replace
- The closest Linux idea to a global setting is a chain's default policy: it applies to every packet no rule matches.
- Lab: server 192.168.243.10 with NET_ADMIN, our own inet table n223 with policy drop.
- nft replace rule keeps the handle and changes only the condition, like Set-NetFirewallPortFilter.
# n223.nft (server, NET_ADMIN)table inet n223 { chain input { type filter hook input priority 0 policy drop tcp dport 22 counter accept }}nft -f /root/n223.nft# client:ping -c 2 -W 1 192.168.243.10 | tail -2# server: default action drop -> acceptnft chain inet n223 input '{ policy accept; }'[client] $ ping -c 2 -W 1 192.168.243.10 | tail -22 packets transmitted, 0 received, 100% packet loss, time 1027ms$ nft chain inet n223 input '{ policy accept; }'$ nft list chain inet n223 inputtable inet n223 { chain input { type filter hook input priority filter; policy accept; tcp dport 22 counter packets 0 bytes 0 accept }}[client] $ ping -c 2 -W 1 192.168.243.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1007msnft chain inet n223 input '{ policy drop; }'nc -zv -w 2 192.168.243.10 2222nft replace rule inet n223 input handle 2 \ tcp dport 2222 counter acceptnft -a list table inet n223nc -zv -w 2 192.168.243.10 2222nft delete table inet n223[client] $ nc -zv -w 2 192.168.243.10 2222nc: connect to 192.168.243.10 port 2222 (tcp) timed out: Operation in progress$ nft replace rule inet n223 input handle 2 tcp dport 2222 counter accept$ nft -a list table inet n223table inet n223 { # handle 2 chain input { # handle 1 type filter hook input priority filter; policy drop; tcp dport 2222 counter packets 0 bytes 0 accept # handle 2 }}[client] $ nc -zv -w 2 192.168.243.10 2222Connection to 192.168.243.10 2222 port [tcp/*] succeeded! tcp dport 2222 counter packets 4 bytes 216 accept # handle 2223.8 Comparison: Set-NetFirewallSetting vs Set-NetFirewallProfile
| Point | Set-NetFirewallProfile | Set-NetFirewallSetting |
|---|---|---|
| Scope | Per profile (Domain, Private, Public) | Global, same for every profile |
| Typical options | -Enabled, -DefaultInboundAction, logging | -Exemptions, -MaxSAIdleTimeSeconds, -CertValidationLevel |
| Example | Domain profile default inbound Block | Exempt ICMP from IPsec requirements |
| Linux analogy | Per-chain policy | Global defaults of the ruleset |
223.9 Common mistakes
- Thinking -WhatIf is always safe: the policy is not changed, but real rules are read and their names appear in the output; get approval on a managed PC.
- Using Set-NetFirewallProfile for an IPsec exemption; exemptions are global, so it is Set-NetFirewallSetting.
- Running Show-NetFirewallRule on an office PC: it reveals the whole policy.
- Missing braces around the GUID in Update-NetFirewallDynamicKeywordAddress -Id.
223.10 Interview tips
- Set-NetFirewallProfile = per profile (default inbound action); Set-NetFirewallSetting = global (exemptions, SA idle time).
- Exempt ICMP on every profile: Set-NetFirewallSetting -Exemptions Icmp in an admin shell.
- IPsec policy: Set-NetIPsecRule plus the crypto set and auth set cmdlets; Show-NetFirewallRule shows the whole policy expanded.
- Sync-NetIPsecRule keeps DirectAccess rule IPs current from DNS; Update-NetIPsecRule edits addresses directly.
- Whole module pattern: a rule is one object; filters, profiles and IPsec sets are objects connected to it.