PowerShell Genie

Find listening TCP ports

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Get-NetTCPConnection with -State Listen returns only sockets sitting in the listening state rather than the full connection table.

The script

1PS> Get-NetTCPConnection -State Listen | Select-Object LocalAddress,LocalPort,OwningProcess

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 LocalAddress LocalPort OwningProcess
    2 ------------ --------- -------------
    3 0.0.0.0 80 4
    4 0.0.0.0 443 4
    5 127.0.0.1 5985 1234
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-NetTCPConnection -State Listen | Select-Object LocalAddress,LocalPort,OwningProcess
    1. Stage 1Get-NetTCPConnection-State
    2. Stage 2Select-ObjectPositional values or no named parameters

      How it works

      Get-NetTCPConnection with -State Listen returns only sockets sitting in the listening state rather than the full connection table. Select-Object keeps the bound address, the port, and OwningProcess, which is the PID of whatever opened it.

      Adapting it

      Resolve that PID inline with a calculated property that calls Get-Process -Id, or filter LocalPort against the range the application owner claims their service uses.

      If the result is unexpected

      The port you expected is absent from the output, or every service appears twice with different local addresses.

      A socket bound to both IP stacks yields separate rows for the IPv4 and IPv6 wildcards, which reads as duplication. A missing port usually means the service bound to a specific address rather than the wildcard, or it is listening on UDP, which this cmdlet does not cover. Sockets in other states also drop out of a listen-only view.

      1. Rerun without the state filter and check whether the port appears in a different state.
      2. Use Get-NetUDPEndpoint for anything that might be a UDP service, since the TCP cmdlet will never show it.
      3. Compare against netstat -ano to confirm the port is genuinely absent rather than filtered out by a parameter.
      4. Check the service's own binding configuration for a specific address instead of a wildcard.
      5. Group on LocalPort to collapse the dual-stack duplication into a single row per port.

      Validate the result

      The port appears once per bound address, and the count of distinct LocalPort values matches the services this host is supposed to expose.

      Keep the check reliable

      Query both the TCP and UDP endpoint cmdlets in any port audit, and deduplicate on port so a dual-stack listener is not counted twice.

      Primary documentation

      Windows services and processes documentation

      Continue
      Genie guideFind listening TCP ports

      Get-NetTCPConnection with -State Listen returns only sockets sitting in the listening state rather than the full connection table.