Search code examples
wmictaskkill

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


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


Solution

  • 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