Search code examples
windowswmic

How to run wmic command in a batch file


I am running couple of wmic commands in a btach file to find a process and killing it.

wmic Path win32_process Where "CommandLine Like '%app1%'" Call Terminate

wmic Path win32_process Where "CommandLine Like '%app2%'" Call Terminate

These commands run fine when I run from console individually but when I run them using a batch file , I get error as below:

wmic Path win32_process Where "CommandLine Like ''" Call Terminate

No Instance(s) Available.

Can someone point out whats the issue with the command if its run from a batch file.


Solution

  • Inside a batch file percent signs need to be escaped. The command you are trying to execute are seeing %app1% as a variable read and replaced with (probably) a empty string.

    You need to use

    wmic Path win32_process Where "CommandLine Like '%%app1%%'" Call Terminate
    

    Note that this condition will also match the current wmic instance, as the search term is also included in its own command line. You should add an aditional test to ensure only the desired process is terminated.