Last active
January 16, 2025 18:46
-
-
Save tcartwright/da629b672ac2283955406d9e69bc51ed to your computer and use it in GitHub Desktop.
POWERSHELL: Get process using TCP port
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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