SkillPilotAI

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

PowerShell
Get-Command -Module NetTCPIP -Verb Set |  Select-Object Name
Output
Name----Set-NetIPAddressSet-NetIPInterfaceSet-NetIPv4ProtocolSet-NetIPv6ProtocolSet-NetNeighborSet-NetOffloadGlobalSettingSet-NetRouteSet-NetTCPSettingSet-NetUDPSetting
CmdletScopeTypical change
Set-NetIPAddressOne addressPrefixLength, PreferredLifetime, ValidLifetime, SkipAsSource
Set-NetIPInterfaceOne interfaceDhcp, InterfaceMetric, NlMtuBytes, Forwarding
Set-NetIPv4Protocol / Set-NetIPv6ProtocolWhole PC (global)DefaultHopLimit, IcmpRedirects, NeighborCacheLimitEntries
Set-NetNeighborOne ARP/ND entryLinkLayerAddress (MAC)
Set-NetRouteOne routeRouteMetric, lifetimes, Publish
Set-NetOffloadGlobalSettingWhole PC (global)ReceiveSideScaling, ReceiveSegmentCoalescing, TaskOffload
Set-NetTCPSettingTCP templateCongestionProvider, InitialCongestionWindowMss, port range
Set-NetUDPSettingUDP globalDynamicPortRangeStartPort / NumberOfPorts
Each Set cmdlet changes one part of the journey
  1. 1.Set-NetIPAddress: prefix / lifetime of the PC's address

  2. 2.Set-NetIPInterface: MTU, metric, DHCP of the interface

  3. 3.Set-NetNeighbor: MAC in the gateway's ARP entry

  4. 4.Set-NetRoute: metric / next hop of the route

  5. 5.Packet goes via the gateway and reaches the server

Your PC -> Gateway -> Server

212.3 Set-NetIPAddress and Set-NetIPInterface

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

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

PowerShell
# 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)

Terminal
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
Output
$ 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
WindowsLinux
Set-NetIPAddress -PreferredLifetimeip addr change ... preferred_lft / valid_lft
Set-NetIPInterface -NlMtuBytesip link set dev eth0 mtu 1400
Set-NetNeighbor -LinkLayerAddressip neigh replace ... lladdr ... nud permanent
Set-NetRouteip route change
Get/Set-NetTCPSettingsysctl net.ipv4.tcp_*
Test-NetConnection -Portnc -zv host port
Terminal
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 8099
Output
net.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 refused

212.7 Test-NetConnection

Test-NetConnection -Port: what it checks
  1. 1.PC sends SYN to 127.0.0.1:18212

  2. 2.Docker listener answers, connection is made

  3. 3.TcpTestSucceeded : True

  4. 4.SYN to port 18299: nobody listens, connection refused

  5. 5.WARNING + TcpTestSucceeded : False

PowerShell
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 Quiet
Output
ComputerName     : 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
OptionMeaning
(no target)Pings internetbeacon.msedge.net by default
-ComputerName host|IPTarget
-Port 443TCP port test
-CommonTCPPort HTTP|RDP|SMB|WINRMWell-known port by name
-InformationLevel QuietOnly True/False
-InformationLevel DetailedAdds DNS, route, IPsec details
-TraceRoute / -HopsEvery hop, like tracert / max hops
-DiagnoseRoutingWhich route and source address Windows chose
-ConstrainInterface / -ConstrainSourceAddressForce an interface / source IP
Output
$ 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 : True

The -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.