PowerShell Get process that is holding a port open

  • Welcome to ITBible, we're your #1 resource for enterprise or homelab IT problems (or just a place to show off your stuff).
Pretty simple script that will answer to what process is using a port.

    [string]$port #e.g. 443

# Pull PID from TCP Connections and then outputs process name(s)

$pids = (Get-NetTCPConnection | Where-Object { $_.LocalPort -eq $port }) | Select-Object -Property OwningProcess

$pids | ForEach-Object { Get-Process -PID $_.OwningProcess | Select-Object -Property Name }
Updated a bit takes the output and keeps it from reporting multiple of the same service.

     Created on:       12/10/2022 12:45 AM
     Created by:       WizardTux
     Organization:     IT Bible (itbible.org)
     Filename: GetProcessByPort.ps1      
        Gets a list of processes using a specific port and outputs the list.

param (
    [Parameter(Mandatory = $true)]
    [string]$Port # e.g. 443

$pids = (Get-NetTCPConnection | Where-Object { $_.LocalPort -eq $Port }) | Select-Object -Property OwningProcess

foreach ($localpid in $pids)
    $current = Get-Process -PID $localpid.OwningProcess | Select-Object -Property ProcessName, Id
    if ($current.ProcessName -ne $lastName)
        $lastName = $current.ProcessName

I want to keep this available as it updates so any future updates will be here.