Created
December 27, 2018 17:36
-
-
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
This file contains 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
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