Search code examples
windowsbatch-filecommand-lineprocesswmic

WMIC ProcessID Batch


I am using WMIC in Batch to get a process ID. How can I use the CommandLine parameter in WMIC Command.

I already tried somthing like this:

for /f "skip=1 tokens=*" %%i in ('wmic process where "name^="Example.exe" and CommandLine like '%Example%'" get ProcessId') do (echo attach %%i >> ".\my-script.txt")    

I get a Process only using the parameter name. But the problem is i have three processes with the same name.


Solution

  • try like this:

    @echo off
    
    for /f "usebackq tokens=* delims=" %%a in (`wmic process where "name='Example.exe' and CommandLine like '%%Example%%'" get ProcessID /Format:value`) do (
        for /f "tokens=* delims=" %%z in ("%%a") do set "%%z"
    )
    echo %processid%
    

    you need to double the % otherwise it will be parsed by cmd.exe as variable.