Skip to content

Instantly share code, notes, and snippets.

@tcartwright
Last active January 16, 2025 18:46
Show Gist options
  • Save tcartwright/da629b672ac2283955406d9e69bc51ed to your computer and use it in GitHub Desktop.
Save tcartwright/da629b672ac2283955406d9e69bc51ed to your computer and use it in GitHub Desktop.
POWERSHELL: Get process using TCP port
Clear-Host
if (!(Get-Module Join-Object)) {
Install-Module -Name Join-Object -Force -AllowClobber -Scope CurrentUser
}
$tcpConnections = Get-NetTCPConnection
# list all ports processes:
$processes = Get-Process -Id ($tcpConnections).OwningProcess -IncludeUserName
# $tcpConnections | Format-List * #OwningProcess
# $processes | Format-List * #id
$merged = Join-Object -Left $tcpConnections -Right $processes `
-LeftJoinProperty OwningProcess -RightJoinProperty Id -Type OnlyIfInBoth `
-LeftProperties LocalAddress, LocalPort, RemoteAddress, RemotePort, State, AppliedSetting, OwningProcess -RightProperties Id, ProcessName, UserName |
Sort-Object LocalPort
$merged | Format-Table
Clear-Host
$tcpConnection = Get-NetTCPConnection -LocalPort 44369 -ErrorAction SilentlyContinue
if ($tcpConnection) {
$tcpConnection | Out-Default
Get-Process -Id $tcpConnection.OwningProcess | Out-Default
} else {
Write-Warning "Could not find port in use"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment