3

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?

  • 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
  • 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
8

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

| improve this answer | |
  • This will also call terminate on wmic.exe itself, since the specified string matches its own command line. – Nicolas Melay Apr 14 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.