How to find PID and taskkill it in one single line in command prompt

52 views Asked by At

I want to close a program using command line. to do so I will use this line of code to find the PID of the service,

wmic process get ProcessID,ExecutablePath | findstr /c:"D:\Eightcap - MT5\terminal64.exe"

and the result is something like this,

D:\Eightcap - MT5\terminal64.exe
20132

Now I want to close this PID using taskkill, how can I use both process in one single line in command prompt?

So far what I could do is using this code,

for /F %a in ('wmic process where ExecutablePath='D:\Eightcap - MT5\terminal64.exe' get ProcessId') do taskkill /pid %a

but it return invalid alias verb.. or if I use this,

for /F %a in ('wmic process where "ExecutablePath='D:\Eightcap - MT5\terminal64.exe'" get ProcessId') do taskkill /pid %a

it returns Description = Invalid query

2

There are 2 answers

0
Luandre Ezra On BEST ANSWER

I found how to do it using this code,

for /F %a in ('wmic process where "ExecutablePath='D:\\Eightcap - MT5\\terminal64.exe'" get ProcessID') do taskkill /pid %a
2
Rakesh Bakoriya On

The following worked for me -

ps -aef | grep <Process name whose PID you want to know>

If you want to know the process of localhost then run the below command.

ps -aef | grep localhost