I need to kill a process which does not list when I use the netstat command. The following command will find the process id of 4 which is not the one that I am looking for.
$processPID = $($(netstat -aon | findstr '5000')[0] -split 's+')[-1]
Stop-Process $processPID
I know the process that I need is in the results of the following.
Get-Process -Name dotnet
And I can’t find a way to use the Get-Process to use with the port filter. For the information of dotnet people, I am running the project using the dotnet CLI command from powershell.
In the screenshot below, you can see the process id for the process listening on port
5100
3
Answers
Thanks to @Mathias and @Sebastian. With your valuable inputs I came up with the following function to get the process id.
Use the
Get-NetTCPConnection
cmdlet to get the same output as netstat, but as structured objects instead of text:As above, here’s a function for getting the PID depending if this is either a LocalPort or RemotePort.