Skip to content

Instantly share code, notes, and snippets.

@winkler-winsen
Last active December 5, 2023 16:17
Show Gist options
  • Save winkler-winsen/f8f7ac2d25bae2c92b05ce20389b2d1a to your computer and use it in GitHub Desktop.
Save winkler-winsen/f8f7ac2d25bae2c92b05ce20389b2d1a to your computer and use it in GitHub Desktop.
Shows established TCP ports and its connected hostnames with local process names
# Lists all active local TCP ports and ask which to check for connections
# https://gist.github.com/winkler-winsen/f8f7ac2d25bae2c92b05ce20389b2d1a
(Get-NetTCPConnection -State Established |
Where-Object -FilterScript { $_.LocalAddress -notlike '127.0.0.1' -and $_.LocalAddress -notlike '::1' } |
Sort-Object -Unique LocalPort |
Select-Object @{L="Proto"; E={"TCP"}},
LocalAddress,
LocalPort,
@{L="Connections"; E={((Get-NetTCPConnection -State Established -LocalPort $_.LocalPort).Count)}},
OwningProcess,
@{L="Process"; E={((Get-Process -ID $_.OwningProcess).Name)}},
@{L="Path"; E={((Get-Process -ID $_.OwningProcess).Path)}}) |
Sort-Object Process, Proto, LocalPort, LocalAddress |
Format-Table -AutoSize
$PortNumber = Read-Host -Prompt "Check witch local port number? "
try {
(Get-NetTCPConnection -State Established -LocalPort $PortNumber | # Insert "-Port 9801" for checking only one special port
Select-Object @{L="Proto"; E={"TCP"}},
RemoteAddress,
@{L="RemoteHost"; E={((Resolve-DnsName $_.RemoteAddress).NameHost)}},
LocalPort,
OwningProcess,
@{L="Process"; E={((Get-Process -ID $_.OwningProcess).Name)}},
@{L="Path"; E={((Get-Process -ID $_.OwningProcess).Path)}},
CreationTime) |
Sort-Object Process, Proto, LocalPort, RemoteAddress, RemoteHost |
Format-Table -AutoSize
}
catch {
Write-Host "No established TCP connections with local port $PortNumber"
}
Read-Host "Enter to exit."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment