6

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?

Revious
  • 355
  • if it's not clear please give me a suggestion so that I can improve the question. – Revious Nov 19 '15 at 14:37
  • 1
    My first thought would be to look up the ID by command-line, and the kill by ID, but I'm not sure I follow exactly what you mean by "use wmic to find the id of a process searching for the command line which was used to start it", so you question is a little unclear. In addition to that, for improving your question to avoid down-votes, I'm thinking people feel it's lacking research effort... What have you tried already? (PS I didn't downvote you ;) ). – Ƭᴇcʜιᴇ007 Nov 19 '15 at 14:54
  • @Ƭᴇcʜιᴇ007 thanks a lot for the help, I don't know exactly how to write it.. I want to write the command in a batch file so I cannot execute wmic, read the output and call taskkill. It must happen by batch.. And I don't know how to use filters like * or % in the search of wmic; I would like something similar to: "wmic process where "CommandLine like %start%", pid | kill that process" – Revious Nov 19 '15 at 15:57
  • The problem is that is very hard to formulate this kind of questions in an elegant way without knowing the nomenclature – Revious Nov 19 '15 at 15:59
  • 1
    Looks like you want xargs. There are some Windows versions out there, for example with Cygwin. – Daniel B Nov 19 '15 at 16:09
  • 1
    @DanielB thanks! but, I cannot solve with the standard windows utities? – Revious Nov 19 '15 at 16:26

1 Answers1

13

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

Now, let's terminate Notepad. Please note the PID is different now. What we do is the following command, a simple sort of sentence-like statement, where we determine the condition and the action, plus the return value so you can check the sanity and success of your administration work.

process where processid="1000" call terminate

enter image description here

Source

Supporting Resource