I have a bad habit of using the NuGet Package Manager Console in Visual Studio as a PoSh CLI (which it sort of is), and today I ran ‘dotnet run’. And then I was unable to kill the session. The console was unresponsive, and so was the NuGet browser. Arghhh….ops. I restarted Visual Studio, and that got the console working again, but my port was taken and Kestrel spinning.
PowerShell has cmdlets for most things you could do in the good ol’ commandline, and there is one for working with TCP connection.
This will list all the connections:
and if you had some filtering with the parameters such as the port you can narrow down the result
Get-NetTCPConnection -Port 1106
The result is a list, and if you wrap the result in parenthesizes you can enumerate the items and pull out specific properties without piping+iteration
(Get-NetTCPConnection -LocalPort 1106).OwningProcess
This gave me the PID (process ID), and with that I could run
$rsult = (Get-NetTCPConnection -LocalPort 1106).OwningProcess Kill -Id $result
Voila. Process dead.