Skip to content

Instantly share code, notes, and snippets.

@mirez
Created December 27, 2018 17:36
Show Gist options
  • Save mirez/066d2e93ff9035a6d4087e6c760df19a to your computer and use it in GitHub Desktop.
Save mirez/066d2e93ff9035a6d4087e6c760df19a to your computer and use it in GitHub Desktop.
Powershell to kill a windows process by the local port it is using
function Stop-PortOwningProcess {
param(
[Parameter(Mandatory, Position=0)]
[int] $localPort )
try{
$netpids = Get-NetTCPConnection | Where-Object{ $_.LocalPort -eq $localPort }
$toBeKilled = Get-Process | Where-Object { $_.Id -eq $netpids[0].OwningProcess }
$toBeKilled
$toBeKilled | %{ Stop-Process -Id $_.Id }
}
catch{ }
finally { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment