SkillPilotAI

PowerShell NetTCPIP module (2/3)

Part 2 of the NetTCPIP module: read open TCP/UDP sockets, inspect TCP/UDP templates and transport filters, then add and remove IPs, neighbors and routes. This is the PowerShell way of doing what netstat and ip do.

211.1 Why is it used?

  • Get-NetTCPConnection / Get-NetUDPEndpoint show open sockets as objects, like netstat, so you can filter by port or state.
  • Get-NetTCPSetting / Get-NetUDPSetting show TCP templates (Internet, Datacenter...) and the dynamic port range.
  • Transport filters decide which template applies to which destination prefix or port range.
  • New-/Remove-NetIPAddress, NetNeighbor, NetRoute add or delete static config from scripts.

211.2 Where it runs

  • NetTCPIP is built into Windows 10/11 and Windows Server; works in Windows PowerShell 5.1 and PowerShell 7.
  • Get- cmdlets only read; New- and Remove- change system state and need an Administrator PowerShell.
  • Linux/macOS do not have this module: ip, ss and netstat do the same job there.
  • In the video only Get- commands were run (on own loopback port 18211); New-/Remove- examples are from Microsoft Learn docs.

211.3 Get-NetTCPConnection and Get-NetUDPEndpoint

  • Windows keeps one row per TCP connection: after the handshake the state is Established; a program that only waits is in Listen.
  • Filter with -LocalAddress, -LocalPort, -RemoteAddress, -RemotePort and -State so only the row you care about is shown.
  • -AppliedSetting tells which TCP template (Internet, Datacenter, Compat...) a connection uses.
  • UDP has no connection and no state, so Get-NetUDPEndpoint only has local address, port, OwningProcess and CreationTime.
TCP connection table
  1. 1.Your PC sends SYN to the web server on :443

  2. 2.Packet goes through the router to the server

  3. 3.Handshake completes, Windows adds a row: Established

  4. 4.A program only waiting for clients has a Listen row

  5. 5.Get-NetTCPConnection returns these rows as objects

Same idea as netstat, but you can filter and Select properties

PowerShell
$ip = [Net.IPAddress]::Parse('127.0.0.1')$t = [Net.Sockets.TcpListener]::new($ip,18211)$t.Start()Get-NetTCPConnection -LocalAddress 127.0.0.1 `  -LocalPort 18211 -State Listen |  Select LocalAddress, LocalPort, State$u = [Net.Sockets.UdpClient]::new(  [Net.IPEndPoint]::new($ip, 18211))Get-NetUDPEndpoint -LocalAddress 127.0.0.1 `  -LocalPort 18211 | Select LocalAddress,  LocalPort | Format-Table$t.Stop(); $u.Close()Get-NetTCPConnection -LocalAddress 127.0.0.1 `  -LocalPort 18211 -State Listen
Output
PS> Get-NetTCPConnection -LocalAddress 127.0.0.1 -LocalPort 18211 -State Listen | Select ...LocalAddress LocalPort  State------------ ---------  -----127.0.0.1        18211 ListenPS> Get-NetUDPEndpoint -LocalAddress 127.0.0.1 -LocalPort 18211 | Select ...LocalAddress LocalPort------------ ---------127.0.0.1        18211PS> $t.Stop(); $u.Close()PS> Get-NetTCPConnection -LocalAddress 127.0.0.1 -LocalPort 18211 -State ListenGet-NetTCPConnection : No matching MSFT_NetTCPConnection objects found by CIM query for instances of the    + CategoryInfo          : ObjectNotFound: (MSFT_NetTCPConnection:String) [Get-NetTCPConnection], CimJobException
  • After the listener stops, the same filter returns an ObjectNotFound error (no matching row), not an empty table.

211.4 TCP and UDP settings (templates)

PowerShell
Get-NetTCPSetting | Select SettingName,  CongestionProvider, InitialRto, MinRto,  AutoTuningLevelLocal | Format-TableGet-NetTCPSetting -SettingName Internet |  Select DynamicPortRangeStartPort,  DynamicPortRangeNumberOfPorts,  MaxSynRetransmissions,  InitialCongestionWindow | Format-ListGet-NetUDPSetting
Output
PS> Get-NetUDPSettingDynamicPortRangeStartPort     : 49152DynamicPortRangeNumberOfPorts : 16384
  • Get-NetTCPSetting returns one row per template. On this PC the Internet template uses CUBIC with InitialRto 1000 ms and MinRto 300 ms.
  • Datacenter has MinRto only 60 ms: the network is fast, so a lost packet should be resent quickly.
  • Docs list CongestionProvider values Default, CTCP, DCTCP, but the real output shows CUBIC and NewReno.
  • Dynamic (ephemeral) port range starts at 49152 with 16384 ports, same for TCP and UDP.
OptionWhat it controls
-CongestionProviderCongestion algorithm (Default/CTCP/DCTCP)
-InitialRtoMs / -MinRtoMsRetransmit timeouts
-MaxSynRetransmissionsHow many times a SYN is retried
-InitialCongestionWindowMssStarting cwnd
-DelayedAckFrequency / -DelayedAckTimeoutMsDelayed ACK behaviour
-AutoTuningLevelLocalReceive window auto-tuning (Normal, Restricted...)
-DynamicPortRangeStartPort / -NumberOfPortsEphemeral source ports
-EcnCapability / -Timestamps / -ForceWSECN, TCP timestamps, window scaling switches

211.5 Transport filters

  • A transport filter maps a destination prefix or local/remote port range to a TCP template.
  • Example: Datacenter template for 192.168.0.0/16, so the DB server gets MinRto 60 ms while internet users stay on Internet (300 ms).
  • Even reading filters needs admin: Get-NetTransportFilter as a normal user returns Access is denied.
  • New-NetTransportFilter needs -SettingName plus a -DestinationPrefix or port range; Remove- also accepts -InputObject from the pipeline.
Transport filter: which template for which traffic
  1. 1.App server opens a connection

  2. 2.Windows checks transport filters for the destination

  3. 3.Destination in 192.168.0.0/16 (DB server): Datacenter template

  4. 4.DB connection gets MinRto 60 ms

  5. 5.Any other destination (internet users): default Internet template, MinRto 300 ms

PowerShell
Get-NetTransportFilter# Example (docs), admin only, NOT run here:New-NetTransportFilter -SettingName Datacenter `  -DestinationPrefix 192.168.0.0/16Remove-NetTransportFilter -SettingName Custom
Output
PS> Get-NetTransportFilterGet-NetTransportFilter : Access is denied.    + FullyQualifiedErrorId : Windows System Error 5,Get-NetTransportFilter

211.6 New-NetIPAddress, New-NetNeighbor, New-NetRoute

Adding a second IP, a neighbor and a route
  1. 1.Windows server has 192.168.1.10

  2. 2.New-NetIPAddress adds 192.168.1.50/24

  3. 3.New-NetNeighbor adds a permanent ARP entry for the router (no ARP request needed)

  4. 4.New-NetRoute: 10.0.0.0/24 via router 192.168.1.1

  5. 5.Packet to 10.0.0.20 goes via the router to the office LAN server

PowerShell
# Example (docs), admin only, NOT run here:New-NetIPAddress -InterfaceIndex 12 `  -IPAddress 192.168.0.1 -PrefixLength 24 `  -DefaultGateway 192.168.0.5New-NetNeighbor -InterfaceIndex 12 `  -IPAddress "192.168.0.5" `  -LinkLayerAddress "00-00-12-00-00-ff"New-NetRoute -DestinationPrefix "10.0.0.0/24" `  -InterfaceIndex 12 -NextHop 192.168.0.1Get-NetRoute | Format-List -Property *
CmdletRequiredUseful options
New-NetIPAddress-IPAddress + -InterfaceIndex/-InterfaceAlias-PrefixLength 24, -DefaultGateway (same subnet), -SkipAsSource, -PolicyStore
New-NetNeighbor-IPAddress + interface-LinkLayerAddress (MAC), -State Permanent/Reachable/...
New-NetRoute-DestinationPrefix + interface-NextHop, -RouteMetric (lower = preferred), -Protocol, -Publish
New-NetTransportFilter-SettingName-DestinationPrefix, -Protocol, local/remote port ranges
  • Docs: adding a static IP automatically disables DHCP on that interface.
  • Docs: the new address is unusable until Duplicate Address Detection (DAD) finishes.
  • -DefaultGateway must be in the same subnet as the new address.

211.7 Remove-NetIPAddress, Remove-NetNeighbor, Remove-NetRoute

PowerShell
# Example (docs), admin only, NOT run here:Remove-NetIPAddress -IPAddress 192.168.0.1 `  -DefaultGateway 192.168.0.5Get-NetIPAddress -IPAddress 192.168.0.1 |  Remove-NetIPAddressRemove-NetNeighbor -State UnreachableRemove-NetRoute -NextHop "192.168.0.1"# docs: removes ALL routes, incl. default:Remove-NetRoute
  • Remove-NetIPAddress with -DefaultGateway removes the gateway too; filter by -PrefixOrigin Manual/Dhcp to target only some addresses.
  • Remove-NetNeighbor -State Unreachable clears only unreachable ARP/ND entries.
  • Remove-NetRoute -NextHop removes the routes of one gateway; with no parameters it removes every route, including the default route.
  • All Remove- cmdlets support -WhatIf / -Confirm and -PassThru.
After Remove-NetRoute: no route at all
  1. 1.Remove-NetRoute deletes 10.0.0.0/24 (and no default route exists)

  2. 2.App sends the next packet to 10.0.0.20

  3. 3.Windows finds no matching route

  4. 4.Packet never reaches the router: no route to 10.0.0.20

211.8 Linux equivalents (Docker lab)

Windows (NetTCPIP)Linux
Get-NetTCPConnection -State Listennetstat -lnt (or ss -lnt)
Get-NetUDPEndpointnetstat -lnu (or ss -lnu)
Get-NetTCPSetting (port range, congestion)/proc/sys/net/ipv4/ip_local_port_range, tcp_congestion_control
New-/Remove-NetIPAddressip addr add / del
New-/Remove-NetNeighborip neigh add / del
New-/Remove-NetRouteip route add / del
Terminal
netstat -lntnetstat -lnucat /proc/sys/net/ipv4/ip_local_port_rangecat /proc/sys/net/ipv4/tcp_congestion_controlip addr add 192.168.231.50/24 dev eth0ip neigh add 192.168.231.77 dev eth0 \  lladdr 02:00:00:00:02:11 nud permanentip route add 192.168.231.128/25 \  via 192.168.231.20 dev eth0ip route showip route get 192.168.231.130
Output
$ cat /proc/sys/net/ipv4/ip_local_port_range32768  60999$ cat /proc/sys/net/ipv4/tcp_congestion_controlcubic$ ip route showdefault via 192.168.231.1 dev eth0192.168.231.0/24 dev eth0 proto kernel scope link src 192.168.231.10192.168.231.128/25 via 192.168.231.20 dev eth0$ ip route get 192.168.231.130192.168.231.130 via 192.168.231.20 dev eth0 src 192.168.231.10 uid 0
  • ss was not used in the lab because the tcp_diag/udp_diag modules were not loaded; netstat reads /proc/net/tcp instead.
  • Linux ephemeral range is 32768-60999 (Windows: 49152 + 16384 ports); congestion control is cubic.
  • Cleanup is the reverse: ip route del, ip neigh del, ip addr del.

211.9 Common mistakes

  • Believing Remove-NetRoute is safe: without parameters it deletes every route, default route included, and the machine goes offline.
  • Running New-/Remove- cmdlets in a non-admin shell and expecting them to work.
  • Dumping all connections on a shared/office PC; filter by -LocalPort / -State instead.
  • Giving a default gateway outside the new address's subnet.

211.10 Interview tips

  • Is anything listening on 8080? Get-NetTCPConnection -LocalPort 8080 -State Listen.
  • Static IP with PowerShell: New-NetIPAddress -InterfaceIndex -IPAddress -PrefixLength -DefaultGateway (DHCP gets disabled automatically); DNS servers via Set-DnsClientServerAddress.
  • Clean up with Remove-NetIPAddress / Remove-NetRoute and verify with Get-NetIPAddress / Get-NetRoute.
  • Always scope Remove-NetRoute with -DestinationPrefix or -NextHop and dry-run with -WhatIf.
  • Datacenter vs Internet template: Datacenter has a much lower MinRto (60 vs 300 ms); transport filters choose the template.