PowerShell NetNat module
NetNat is the PowerShell module for WinNAT, the NAT built into Windows 10/11 and Windows Server. Hyper-V VMs and containers on an internal switch reach the outside network through it, and it can also port-forward inbound traffic to them.
231.1 Why is it used?
- Hides the private prefix of an internal Hyper-V/container switch behind the host's own IP (like a home router).
- Gives VMs outbound access without a separate router VM.
- Static mappings publish a service inside (e.g. VM web on :80) on a host port (e.g. :8080).
- Runs in Windows PowerShell 5.1 and PowerShell 7; New/Add/Set/Remove need an admin shell and change the host's NAT directly.
231.2 How it works
- Step 1
1.VM1 172.16.0.10:50001 sends a request to 192.168.1.20:80
- Step 2
2.Packet goes to its gateway: host vEthernet 172.16.0.1
- Step 3
3.WinNAT rewrites the source to the host IP 192.168.1.50:10000
- Step 4
4.The translation is stored as a NAT session (Get-NetNatSession)
- Step 5
5.Reply arrives at 192.168.1.50:10000
- Step 6
6.WinNAT looks up the session and changes the destination back to 172.16.0.10:50001
Outbound = source NAT (SNAT / masquerade)
- Step 1
1.Remote machine 192.168.1.20 connects to host 192.168.1.50:8080
- Step 2
2.Add-NetNatStaticMapping rule matches :8080
- Step 3
3.WinNAT rewrites the destination to VM1 172.16.0.10:80
- Step 4
4.VM1's web service answers; the reply is translated back
Inbound = destination NAT (DNAT)
231.3 What was run on Windows (read-only)
- The office PC runs Docker and WSL, so not even Get-NetNat / Get-NetNatSession was run (they could show internal prefixes and live connections).
- Only the cmdlet count and the local help were checked; all other examples come from Microsoft Learn.
Get-Command -Module NetNat | Measure-ObjectGet-Help New-NetNat$ Get-Command -Module NetNat | Measure-ObjectCount : 13 $ Get-Help New-NetNatNAME New-NetNatSYNTAX New-NetNat [-Name] <string> [-ExternalIPInterfaceAddressPrefix <string>] [-InternalRoutingDomainId <string>] [-InternalIPInterfaceAddressPrefix <string>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob] [-WhatIf] [-Confirm] [<CommonParameters>]ALIASES NoneREMARKS Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.- 13 cmdlets, matching the 13 cmdlet sections of the docs.
- Only -Name is required for New-NetNat; both prefix parameters are optional.
231.4 Common parameters
| Parameter | What it does |
|---|---|
| -AsJob | Runs the command as a background job |
| -CimSession | Runs it on a remote computer / session (e.g. Server01) |
| -Confirm | Asks for confirmation first |
| -ThrottleLimit | Maximum number of concurrent operations |
| -WhatIf | Shows what would happen; the cmdlet is not run |
231.5 New-NetNat and the Get cmdlets
# Hyper-V NAT network (Microsoft Learn setup guide)New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24 # docs examples: external side, and a tenant routing domainNew-NetNat -Name "AllTenants" -ExternalIPInterfaceAddressPrefix "a.b.c.0/24"New-NetNat -Name "TSQATenant" -ExternalIPInterfaceAddressPrefix "a.b.c.0/24" ` -InternalRoutingDomainId "{bb47986c-f134-4a29-ad87-24010bf2c92f}"- -InternalIPInterfaceAddressPrefix: the inside prefix that gets NAT.
- -ExternalIPInterfaceAddressPrefix: the outside side.
- -InternalRoutingDomainId: GUID of a tenant's routing domain.
- Note from the guide: a host can currently have only ONE NAT network.
Get-NetNat # NAT objects + internal prefixGet-NetNat -Name "MyName"Get-NetNatExternalAddress -NatName "value" # external IP + port rangeGet-NetNatGlobal # global settings of all NAT instancesGet-NetNatStaticMapping -NatName "value" # port forward rulesGet-NetNatSession # all active NAT sessions (live)231.6 Add / Remove external addresses and static mappings
Add-NetNatExternalAddress -IPAddress "a.b.c.0/24" -PortEnd 19999 -PortStart 10000Remove-NetNatExternalAddress -NatName "value" -ExternalAddressID 1- -IPAddress and -NatName are required; -PortStart/-PortEnd set the port range used for outbound connections.
- -ExternalAddressID is the ID shown by Get-NetNatExternalAddress.
- Remove-NetNatExternalAddress with no filter removes it from ALL NAT instances.
Add-NetNatStaticMapping -ExternalIPAddress "a.b.c.0/24" -ExternalPort 80 ` -InternalIPAddress "192.0.02.179" -InternalPort 8080 ` -InternalRoutingDomainId "{12345678-0000-0000-0000-123456789012}"Remove-NetNatStaticMapping -NatName "value" -StaticMappingID 1- -NatName and -Protocol (TCP or UDP) are required.
- If -InternalPort is omitted, NAT uses the same port as -ExternalPort.
- Remove-NetNatStaticMapping with no parameter removes ALL static mappings.
231.7 Set-NetNat, Remove-NetNat, Set-NetNatGlobal
Set-NetNat -Name "TSQATenant" -TcpFilteringBehavior AddressDependentFiltering -UdpInboundRefresh $TrueGet-NetNat | Remove-NetNatRemove-NetNat -Name "MyName" -PassThruSet-NetNatGlobal -InterRoutingDomainHairpinningMode Local| Cmdlet / parameter | Meaning |
|---|---|
| -TcpFilteringBehavior / -UdpFilteringBehavior | EndpointIndependentFiltering (default) or AddressDependentFiltering |
| -UdpInboundRefresh $True | Inbound packets also refresh a UDP session |
| Timeouts (seconds) | -IcmpQueryTimeout, -UdpIdleSessionTimeout, -TcpEstablishedConnectionTimeout, -TcpTransientConnectionTimeout |
| Remove-NetNat | Removes a NAT object; existing connections through it are dropped |
| -InterRoutingDomainHairpinningMode | Local = route tenant-to-tenant traffic within the subnet; External = via external interface (default) |
231.8 Linux equivalent lab (nftables + conntrack)
Docker lab: client 192.168.251.10, router 192.168.251.2 / 192.168.252.2 (ip_forward=1), server 192.168.252.20.
| WinNAT cmdlet | Linux equivalent |
|---|---|
| New-NetNat | nft postrouting chain with masquerade |
| Get-NetNatSession | conntrack -L |
| Add-NetNatStaticMapping | nft prerouting chain with dnat |
| Remove-NetNat | nft delete table |
Without NAT: the server sees the private source but has no route back, so the connection times out.
# client: connect to the outside server without NATnc -v -d -w 3 -p 40001 192.168.252.20 80[server]$ cat /tmp/t1.txt15:19:32.863738 IP 192.168.251.10.40001 > 192.168.252.20.80: Flags [S], seq 1294994274, win 64240, options [mss 1460,sackOK,TS val 2779640001 ecr 0,nop,wscale 10], length 0[client]$ nc -v -d -w 3 -p 40001 192.168.252.20 80nc: connect to 192.168.252.20 port 80 (tcp) timed out: Operation in progress(exit 1)# router: New-NetNat ideanft add table ip n231natnft add chain ip n231nat postrouting '{ type nat hook postrouting priority srcnat; }'nft add rule ip n231nat postrouting ip saddr 192.168.251.0/24 oifname eth1 counter masquerade# client: connect againnc -v -d -w 3 -p 40002 192.168.252.20 80[client]$ nc -v -d -w 3 -p 40002 192.168.252.20 80Connection to 192.168.252.20 80 port [tcp/http] succeeded!outside server saw 192.168.252.2 port 40002(exit 0)conntrack -L -p tcp --dport 80conntrack v1.4.8 (conntrack-tools): 2 flow entries have been shown.tcp 6 111 TIME_WAIT src=192.168.251.10 dst=192.168.252.20 sport=40002 dport=80 src=192.168.252.20 dst=192.168.252.2 sport=80 dport=40002 [ASSURED] mark=0 use=1tcp 6 90 SYN_SENT src=192.168.251.10 dst=192.168.252.20 sport=40001 dport=80 [UNREPLIED] src=192.168.252.20 dst=192.168.251.10 sport=80 dport=40001 mark=0 use=1- Line 1 (port 40002): reply side dst=192.168.252.2, so the source was translated.
- Line 2 (port 40001, before NAT): UNREPLIED, reply dst still the private address.
# router: Add-NetNatStaticMapping ideanft add chain ip n231nat prerouting '{ type nat hook prerouting priority dstnat; }'nft add rule ip n231nat prerouting iifname eth1 tcp dport 8080 counter dnat to 192.168.251.10:80# server: connect to the router's port 8080nc -v -d -w 3 192.168.252.2 8080before the rule:nc: connect to 192.168.252.2 port 8080 (tcp) failed: Connection refusedafter the rule:Connection to 192.168.252.2 8080 port [tcp/http-alt] succeeded!inside client web saw 192.168.252.20 port 55352[router]$ conntrack -L -p tcp --dport 8080tcp 6 117 TIME_WAIT src=192.168.252.20 dst=192.168.252.2 sport=55352 dport=8080 src=192.168.251.10 dst=192.168.252.20 sport=80 dport=55352 [ASSURED] mark=0 use=1- dnat changes only the destination, so the inside web sees the real server address 192.168.252.20.
nft delete table ip n231nat # Remove-NetNat idea[client]$ nc -v -d -w 3 -p 40003 192.168.252.20 80nc: connect to 192.168.252.20 port 80 (tcp) timed out: Operation in progress[server]$ nc -v -d -w 3 192.168.252.2 8080nc: connect to 192.168.252.2 port 8080 (tcp) failed: Connection refused231.9 Common mistakes
- Running Remove-NetNat without -Name: it removes ALL NAT objects and drops their connections (can break Docker/WSL networking).
- Creating a second NAT on a host that already has one (Docker/WSL): New-NetNat errors; check Get-NetNat first and remove only your own.
- Forgetting -Protocol / -NatName on Add-NetNatStaticMapping.
- Running New/Add/Set/Remove in a non-admin shell.
231.10 Real-world example: NAT for a Hyper-V VM
New-VMSwitch -SwitchName NATSwitch -SwitchType InternalNew-NetIPAddress -IPAddress 172.16.0.1 -PrefixLength 24 -InterfaceAlias "vEthernet (NATSwitch)"New-NetNat -Name NATNet -InternalIPInterfaceAddressPrefix 172.16.0.0/24Add-NetNatStaticMapping -NatName NATNet -Protocol TCP -ExternalIPAddress 0.0.0.0 ` -ExternalPort 8080 -InternalIPAddress 172.16.0.10 -InternalPort 80231.11 Interview tips
- Hyper-V VM with NAT: New-VMSwitch Internal -> New-NetIPAddress 172.16.0.1/24 on the host -> New-NetNat 172.16.0.0/24 -> Add-NetNatStaticMapping -> verify with Get-NetNatSession.
- Outbound WinNAT = SNAT/masquerade; static mapping = DNAT/port forward.
- One host = one NAT network (per the docs).
- Remove-NetNat without -Name removes everything; always pass -Name.
- Linux mapping: masquerade, dnat, conntrack -L.