How to use wmic to kill a cmd.exe instance searching by it's command line?

I have seen that is possible to use wmic to find the id of a process searching for the command line which was used to start it.

But I have 2 problems.

  • I need to use this in a batch file so I cannot read the output of the wmic and call the taskkill I must use some kind of grep
  • I couldn't find how to search for a partial match. I prefer to search for a piece of the commandline.

What I could find is this example:

wmic process where "CommandLine=start.bat", pid

But what I would like to do is more similar to:

wmic process where "CommandLine like %start.bat%", pid | taskkill 

This is what I mean by command line: "cmd.exe" /c C:\Mysql\start.bat"

Is there a way to kill a process searching for the command line which started it?


Solution 1:

How to use wmic to kill a cmd.exe instance searching by it's command line?

It seems that you can do this natively from Windows batch with the below format. Just plug in your batch file name (which is the string found with WMIC COMMANDLINE option) in the %start.bat% part as I did below—I used start.bat in my example below just as you used in your example.

WMIC KILL SPECIFIC BATCH FILE PROCESS EXAMPLE

WMIC PROCESS WHERE "COMMANDLINE LIKE '%start.bat%'" CALL TERMINATE

enter image description here

Further Reading and Resource URL: http://www.dedoimedo.com/computers/windows-wmic.html