PowerShell NetTCPIP module (3/3)
Part 3 of the NetTCPIP module: the nine Set-Net* cmdlets that change live IP, interface, neighbor, route and TCP/UDP settings, plus Test-NetConnection for checking a TCP port. Set- is how you tune Windows networking from a script; Test-NetConnection is how you verify it.
212.1 Why is it used?
- Change properties of existing config (prefix, lifetime, MTU, metric, MAC, TCP template) without deleting and recreating it.
- Make one interface preferred over another (Ethernet over Wi-Fi) with metrics.
- Test-NetConnection -Port checks if a TCP port is reachable; -InformationLevel Quiet gives just True/False for scripts.
- Set- cmdlets need an Administrator PowerShell and a wrong value can take the network down immediately.
212.2 The nine Set cmdlets
Get-Command -Module NetTCPIP -Verb Set | Select-Object NameName----Set-NetIPAddressSet-NetIPInterfaceSet-NetIPv4ProtocolSet-NetIPv6ProtocolSet-NetNeighborSet-NetOffloadGlobalSettingSet-NetRouteSet-NetTCPSettingSet-NetUDPSetting| Cmdlet | Scope | Typical change |
|---|---|---|
| Set-NetIPAddress | One address | PrefixLength, PreferredLifetime, ValidLifetime, SkipAsSource |
| Set-NetIPInterface | One interface | Dhcp, InterfaceMetric, NlMtuBytes, Forwarding |
| Set-NetIPv4Protocol / Set-NetIPv6Protocol | Whole PC (global) | DefaultHopLimit, IcmpRedirects, NeighborCacheLimitEntries |
| Set-NetNeighbor | One ARP/ND entry | LinkLayerAddress (MAC) |
| Set-NetRoute | One route | RouteMetric, lifetimes, Publish |
| Set-NetOffloadGlobalSetting | Whole PC (global) | ReceiveSideScaling, ReceiveSegmentCoalescing, TaskOffload |
| Set-NetTCPSetting | TCP template | CongestionProvider, InitialCongestionWindowMss, port range |
| Set-NetUDPSetting | UDP global | DynamicPortRangeStartPort / NumberOfPorts |
- Step 1
1.Set-NetIPAddress: prefix / lifetime of the PC's address
- Step 2
2.Set-NetIPInterface: MTU, metric, DHCP of the interface
- Step 3
3.Set-NetNeighbor: MAC in the gateway's ARP entry
- Step 4
4.Set-NetRoute: metric / next hop of the route
- Step 5
5.Packet goes via the gateway and reaches the server
Your PC -> Gateway -> Server
212.3 Set-NetIPAddress and Set-NetIPInterface
# Example (docs), admin only, NOT run here:New-NetIPAddress -InterfaceIndex 12 ` -IPAddress 192.168.0.1Set-NetIPAddress -InterfaceIndex 12 ` -IPAddress 192.168.0.1 -PrefixLength 24$Timesp = ( New-TimeSpan -Days 1 )Set-NetIPAddress -InterfaceIndex 12 ` -IPAddress 192.168.0.1 ` -PreferredLifetime $TimespSet-NetIPInterface -InterfaceIndex 12 ` -Dhcp DisabledGet-NetAdapter | Where-Object -FilterScript ` {$_.LinkSpeed -Eq "100 Mbps"} | Set-NetIPInterface -InterfaceMetric 21- Set-NetIPAddress: -IPAddress, -InterfaceIndex, -AddressFamily only select which address; -PrefixLength, lifetimes and -SkipAsSource are what change.
- Set-NetIPInterface (36 options), group 1: -Dhcp, -InterfaceMetric, -NlMtuBytes, -Forwarding Enabled (PC forwards like a router), IPv6 router advertisement options.
- Group 2: neighbor discovery timers (DAD, ReachableTime), -WeakHostSend / -WeakHostReceive for multi-homed servers, -ClampMss, -EcnMarking, Wake-on-LAN patterns.
212.4 Global protocol, offload, TCP and UDP settings
# Example (docs), admin only, NOT run here:Set-NetIPv4Protocol -MediaSenseEventLog EnabledSet-NetIPv4Protocol -NeighborCacheLimitEntries 1000Set-NetIPv6Protocol -MediaSenseEventLog EnabledSet-NetOffloadGlobalSetting ` -ReceiveSegmentCoalescing EnabledSet-NetTCPSetting ` -SettingName "InternetCustom" ` -CongestionProvider CTCP ` -InitialCongestionWindowMss 6Set-NetUDPSetting ` -DynamicPortRangeStartPort 49152 ` -DynamicPortRangeNumberOfPorts 1700- IPv4/IPv6 protocol cmdlets are global for the whole PC, not one adapter. -DefaultHopLimit sets the TTL of new packets; -IcmpRedirects and -SourceRoutingBehavior are switched off for security.
- IPv6 has MLD in place of IGMP, plus temporary (privacy) address options like -UseTemporaryAddresses.
- Offload: RSS spreads incoming traffic over CPU cores, RSC combines small segments (less CPU), TaskOffload moves checksums to the NIC, NetworkDirect = RDMA.
- Set-NetTCPSetting edits a template (InternetCustom/DatacenterCustom): congestion provider, cwnd, RTO, SYN retries, port range.
- Set-NetUDPSetting has only 5 options: two for the dynamic port range, plus -InputObject and -PassThru.
212.5 Set-NetNeighbor and Set-NetRoute
# Example (docs), admin only, NOT run here:Set-NetNeighbor -InterfaceIndex 12 ` -IPAddress "192.168.0.5" ` -LinkLayerAddress "00-00-12-00-00-ff"Set-NetRoute -RouteMetric 257$TimeSpan = New-TimeSpan -Days 1Set-NetRoute ` -DestinationPrefix "192.168.0.0/24" ` -PreferredLifetime $TimeSpan- Set-NetNeighbor: IPAddress + interface select the entry, -LinkLayerAddress gives the new MAC.
- Set-NetRoute -RouteMetric 257 makes a route slightly less preferred (default metric is 256).
- -Publish decides if the route goes into router advertisements; -Protocol filters by source (NetMgmt, Dhcp, Ospf, Bgp...).
212.6 Linux equivalents (Docker lab)
ip addr add 192.168.232.50/24 dev eth0ip addr change 192.168.232.50/24 dev eth0 \ preferred_lft 3600 valid_lft 7200ip link set dev eth0 mtu 1400ip neigh replace 192.168.232.20 \ lladdr 2e:42:5b:63:23:ca dev eth0 \ nud permanentip route add 192.168.232.128/25 dev eth0 \ metric 300ip route change 192.168.232.128/25 \ via 192.168.232.20 dev eth0 metric 300$ ip -4 addr show dev eth0 inet 192.168.232.10/24 brd 192.168.232.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.232.50/24 scope global secondary dynamic eth0 valid_lft 7199sec preferred_lft 3599sec$ ip link set dev eth0 mtu 1400$ ip link show dev eth0 | head -12: eth0@if1649: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1400 ...$ ip neigh replace 192.168.232.20 lladdr 2e:42:5b:63:23:ca dev eth0 nud permanent$ ip neigh show 192.168.232.20192.168.232.20 dev eth0 lladdr 2e:42:5b:63:23:ca PERMANENT$ ip route show192.168.232.0/24 dev eth0 proto kernel scope link src 192.168.232.10192.168.232.128/25 via 192.168.232.20 dev eth0 metric 300| Windows | Linux |
|---|---|
| Set-NetIPAddress -PreferredLifetime | ip addr change ... preferred_lft / valid_lft |
| Set-NetIPInterface -NlMtuBytes | ip link set dev eth0 mtu 1400 |
| Set-NetNeighbor -LinkLayerAddress | ip neigh replace ... lladdr ... nud permanent |
| Set-NetRoute | ip route change |
| Get/Set-NetTCPSetting | sysctl net.ipv4.tcp_* |
| Test-NetConnection -Port | nc -zv host port |
sysctl net.ipv4.tcp_congestion_control \ net.ipv4.tcp_syn_retries net.ipv4.tcp_ecn \ net.ipv4.tcp_timestamps \ net.ipv4.ip_local_port_rangenc -zv -w 2 192.168.232.20 8080nc -zv -w 2 192.168.232.20 8099net.ipv4.tcp_congestion_control = cubicnet.ipv4.tcp_syn_retries = 6net.ipv4.tcp_ecn = 2net.ipv4.tcp_timestamps = 1net.ipv4.ip_local_port_range = 32768 60999$ nc -zv -w 2 192.168.232.20 8080Connection to 192.168.232.20 8080 port [tcp/http-alt] succeeded!$ nc -zv -w 2 192.168.232.20 8099nc: connect to 192.168.232.20 port 8099 (tcp) failed: Connection refused212.7 Test-NetConnection
- Step 1
1.PC sends SYN to 127.0.0.1:18212
- Step 2
2.Docker listener answers, connection is made
- Step 3
3.TcpTestSucceeded : True
- Step 4
4.SYN to port 18299: nobody listens, connection refused
- Step 5
5.WARNING + TcpTestSucceeded : False
Test-NetConnection 127.0.0.1 -Port 18212 | Select-Object ComputerName,RemoteAddress, RemotePort,TcpTestSucceededTest-NetConnection 127.0.0.1 -Port 18299 | Select-Object ComputerName,RemoteAddress, RemotePort,TcpTestSucceededTest-NetConnection 127.0.0.1 -Port 18212 ` -InformationLevel QuietTest-NetConnection 127.0.0.1 -Port 18299 ` -InformationLevel QuietComputerName : 127.0.0.1RemoteAddress : 127.0.0.1RemotePort : 18212TcpTestSucceeded : TrueWARNING: TCP connect to (127.0.0.1 : 18299) failedComputerName : 127.0.0.1RemoteAddress : 127.0.0.1RemotePort : 18299TcpTestSucceeded : False$ Test-NetConnection 127.0.0.1 -Port 18212 -InformationLevel QuietTrue$ Test-NetConnection 127.0.0.1 -Port 18299 -InformationLevel QuietFalse$ docker rm -f n212-listen$ Test-NetConnection 127.0.0.1 -Port 18212 -InformationLevel QuietFalse| Option | Meaning |
|---|---|
| (no target) | Pings internetbeacon.msedge.net by default |
| -ComputerName host|IP | Target |
| -Port 443 | TCP port test |
| -CommonTCPPort HTTP|RDP|SMB|WINRM | Well-known port by name |
| -InformationLevel Quiet | Only True/False |
| -InformationLevel Detailed | Adds DNS, route, IPsec details |
| -TraceRoute / -Hops | Every hop, like tracert / max hops |
| -DiagnoseRouting | Which route and source address Windows chose |
| -ConstrainInterface / -ConstrainSourceAddress | Force an interface / source IP |
$ Test-NetConnection -ComputerName www.contoso.com -DiagnoseRouting -InformationLevel DetailedComputerName : www.contoso.comRemoteAddress : 2001:428:3805:187::2768SelectedSourceAddress : 2001:4898:e0:79:f17c:d212:8743:43c2OutgoingInterfaceIndex : 4SelectedNetRoute : DestinationPrefix: ::/0 NextHop: fe80::200:5eff:fe00:202RouteDiagnosticsSucceeded : TrueThe -DiagnoseRouting output above is the example output from the Microsoft Learn docs, not a run on this PC.
212.8 Common mistakes
- Thinking Set-NetIPAddress gives a new IP: -IPAddress only selects which address; to change the address use Remove-NetIPAddress + New-NetIPAddress.
- Running Test-NetConnection with no target on an office PC: it contacts internetbeacon.msedge.net; always give your own target.
- Changing global settings (offload, IPv4 protocol) while debugging one adapter: they affect the whole PC.
- Parsing the full Test-NetConnection object in an if: use -InformationLevel Quiet for a boolean.
212.9 Interview tips
- Is port 443 open, only True/False? Test-NetConnection srv -Port 443 -InformationLevel Quiet.
- Prefer Ethernet over Wi-Fi: Windows adds route metric + interface metric and the lowest total wins, so lower Ethernet's InterfaceMetric (Set-NetIPInterface) or raise the Wi-Fi RouteMetric (Set-NetRoute).
- All Set-Net* cmdlets need admin and apply live; know which are global (IPv4/IPv6 protocol, offload, TCP/UDP) vs per object.
- Linux equivalents: ip addr change, ip link set mtu, ip neigh replace, ip route change, sysctl, nc -zv.