EclipseRMM green eclipse symbolEclipseRMM

FIELD NOTES / WINDOWS DEVICE MANAGEMENT

How to Find the Process Using a Port with PowerShell (Get-NetTCPConnection)

To find the process using a port with PowerShell, ask Get-NetTCPConnection for the port and read its OwningProcess value, then pass that process ID (PID) to Get-Process: Get-Process -Id (Get-NetTCPConnection -LocalPort 8080 -State Listen).OwningProcess. That answers the usual question, "what is already listening on this port?", in one line. This guide builds on it: UDP ports, services hidden inside a shared host process, the executable path, the same check on remote computers, and what to verify before you stop anything.

Step 1: find the owning process ID for the port

Microsoft describes Get-NetTCPConnection as the cmdlet that "gets current TCP connections", with properties such as local and remote address, local and remote port, and connection state. Its -LocalPort parameter filters by port, -State filters by TCP state, and -OwningProcess is documented as "the PID of the owning process of a TCP connection".

A program that has opened a port and is waiting for connections is in the Listen state, so filter on that:

Get-NetTCPConnection -LocalPort 8080 -State Listen -ErrorAction SilentlyContinue |
    Select-Object LocalAddress, LocalPort, State, OwningProcess

You may see more than one line for the same port, for example one for an IPv4 address and one for IPv6. If the port is free, the command returns nothing; -ErrorAction SilentlyContinue keeps that case quiet instead of printing an error.

Drop -State Listen when you want every connection on the port, including established ones, or use -RemotePort to find which local process is talking to a port on another machine, such as outbound connections to port 443.

Step 2: turn the PID into a process name

Get-Process -Id accepts one or more PIDs. Combine the two steps and remove duplicate PIDs first:

$port = 8080
Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue |
    Select-Object -ExpandProperty OwningProcess -Unique |
    ForEach-Object {
        $proc = Get-Process -Id $_ -ErrorAction SilentlyContinue
        [pscustomobject]@{
            Port        = $port
            PID         = $_
            ProcessName = $proc.ProcessName
        }
    }

This read-only check is safe to run anywhere. A process can exit between the two commands, which is why Get-Process also uses -ErrorAction SilentlyContinue; an empty process name means the PID no longer exists.

Step 3: see which service is behind the process

Many Windows services share a host process, so the process name alone (for example svchost) may not tell you what owns the port. The Win32_Service class has a ProcessId property, documented as the "process identifier of the service", so you can list the services running in that PID:

$owningPid = 1234   # the PID from step 1
Get-CimInstance -ClassName Win32_Service -Filter "ProcessId = $owningPid" |
    Select-Object Name, DisplayName, State, StartMode

If one service comes back, that service owns the port. If several do, they share the process and you need the program's own documentation or configuration to know which one opened it. Our guide on how to check service status with PowerShell covers reading a service's start mode, account and exit code once you know its name.

Step 4: get the executable path and command line

A process name can be misleading, so check the file that is actually running. The Win32_Process class exposes ExecutablePath ("path to the executable file of the process") and CommandLine ("command line used to start a specific process, if applicable"):

Get-CimInstance -ClassName Win32_Process -Filter "ProcessId = $owningPid" |
    Select-Object ProcessId, Name, ExecutablePath, CommandLine, CreationDate

The class reference lists SeDebugPrivilege for ExecutablePath, and the Get-Process reference says you must run PowerShell as administrator to read module and file version details for processes you don't own. If the path comes back empty, run the same command from an elevated session. To see who started the process, Microsoft's Get-Process documentation shows two options: Get-Process -Id $owningPid -IncludeUserName, or the GetOwner method of Win32_Process through Invoke-CimMethod.

When the executable is unexpected, check whether it is signed before you trust it; our guide on verifying a digital signature with PowerShell shows how.

UDP ports: use Get-NetUDPEndpoint

UDP has no connections or listening state, so Get-NetTCPConnection won't show it. Get-NetUDPEndpoint "gets current statistics for a UDP endpoint", and its -OwningProcess parameter is "the PID of the owning process of a bound UDP socket". The pattern is the same:

Get-NetUDPEndpoint -LocalPort 5353 -ErrorAction SilentlyContinue |
    Select-Object LocalAddress, LocalPort, OwningProcess

The netstat alternative

On computers where you prefer the classic tool, netstat gives the same answer. Microsoft documents -a (all connections and listening ports), -n (numeric addresses and ports), and -o (include the process ID for each connection). Filter its text output in PowerShell:

netstat -ano | Select-String ':8080\s'

netstat -b shows the executable involved in each connection or listening port, but the documentation warns that it "can be time-consuming and will fail unless you have sufficient permissions". The PowerShell cmdlets are easier to script because they return objects with a numeric OwningProcess property instead of text you have to parse.

Check a port on remote computers

Get-NetTCPConnection has a -CimSession parameter, which "runs the cmdlet in a remote session or on a remote computer", so the port lookup works directly against a computer name:

Get-NetTCPConnection -CimSession PC01 -LocalPort 8080 -State Listen

The Get-Process reference says that to get process information from a remote computer you should use Invoke-Command. To resolve the process name as well, run the whole lookup on each computer and collect the results:

$computers = 'PC01', 'PC02', 'PC03'
$port      = 8080

$results = Invoke-Command -ComputerName $computers -ErrorAction SilentlyContinue -ScriptBlock {
    Get-NetTCPConnection -LocalPort $Using:port -State Listen -ErrorAction SilentlyContinue |
        Select-Object -ExpandProperty OwningProcess -Unique |
        ForEach-Object {
            $proc = Get-CimInstance -ClassName Win32_Process -Filter "ProcessId = $_"
            [pscustomobject]@{
                PID            = $_
                ProcessName    = $proc.Name
                ExecutablePath = $proc.ExecutablePath
            }
        }
}

$results | Select-Object PSComputerName, PID, ProcessName, ExecutablePath |
    Sort-Object PSComputerName

Computers where nothing listens on the port return no rows, and computers that didn't answer are missing from the results too, so compare the output with your list before you conclude that a port is free everywhere. Invoke-Command needs PowerShell remoting (WinRM); if that isn't available on your workstations, see PowerShell remote management without RDP for the alternatives.

Before you stop the process

Once you know the owner, the fix is usually to stop or reconfigure the program or service properly, not to kill the process. If you do decide to end it, keep these points from Microsoft's Stop-Process reference in mind:

Running the port check through an RMM agent

With EclipseRMM's PowerShell RMM, you run authorized PowerShell such as the step 2 script on enrolled Windows devices and review the output, errors and completion status in the portal, without relying on WinRM. The Windows agent service runs as LocalSystem, so the lookup can read details of processes owned by other accounts; that same privilege is a reason to keep the investigation read-only and review any stop command before you submit it. A device that is disconnected cannot be assumed to have run a request, so check the completion record; RMM heartbeat monitoring shows which devices are offline. Our list of safe first PowerShell commands follows the same read-first approach.

FAQ: find the process using a port in PowerShell

What is the PowerShell equivalent of netstat -ano?

Get-NetTCPConnection for TCP and Get-NetUDPEndpoint for UDP. Both return an OwningProcess property, which is the same PID that netstat -o shows.

How do I find which process is listening on port 443 or 80?

Run Get-NetTCPConnection -LocalPort 443 -State Listen (or 80) and pass the OwningProcess value to Get-Process -Id. If the result is a shared host process, use the Win32_Service query in step 3.

Why is the executable path empty?

Reading details of processes you don't own needs elevated rights. Run PowerShell as administrator and query Win32_Process again.

Can I free a port without stopping the process?

Usually only by changing the program's configuration to use another port, or by stopping the service that owns it cleanly. Check the program's own documentation.