How to run wmic command in batch file

I run a couple of wmic commands on the btach file to find the process and kill it.

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

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

These commands work fine when I run the console separately, but when I run them using a batch file, I get an error like below:

wmic Path win32_process Where CommandLine Like End Call

No copies.

Can someone point out what the problem is with the command if run from a batch file.

+3


source to share


1 answer


Within a batch file, percent signs must be escaped. The command you are trying to execute sees %app1%

as a variable read and replaced with (possibly) an 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 instance wmic

as the search term is also included in its own command line. You must add an additional test to ensure that only the desired process completes.

+5


source







All Articles