Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run wmic command in a batch file

Tags:

windows

wmic

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.

like image 581
Bankelaal Avatar asked Nov 26 '14 03:11

Bankelaal


People also ask

How do I run a WMIC command?

Open a command prompt. Type WMIC to invoke the program, and hit enter. This will give you the WMIC command prompt, wmic:root\cli> From here, you can run WMI queries.

What is WMIC command in CMD?

The WMI command-line (WMIC) utility provides a command-line interface for Windows Management Instrumentation (WMI). WMIC is compatible with existing shells and utility commands.

Is WMI and WMIC the same?

What is WMIC? The Windows Management Instrumentation (WMI) Command-Line Utility (WMIC) is a command-line utility that allows users to perform WMI operations from a command prompt. WMI is an interface providing a variety of Windows management functions.

How do I check my WMIC?

Wmic is an external command that is available for the following Microsoft operating systems. In Windows 10 and Windows 11, it is located at C:\Windows\System32\wbem\WMIC.exe.


1 Answers

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.

like image 104
MC ND Avatar answered Sep 29 '22 05:09

MC ND