Search code examples
windowsbatch-filecmdwmic

Use variable in wmic command inside for loop in Batch file


I am unable to get this query to work:

set APPNAME="App Name"

for /f "skip=1delims= " %%t in ('wmic product where "name=%APPNAME%" get IdentifyingNumber') do set "guid=%%t"& goto printguid
:printguid
    echo %guid%

I am getting "Invalid alias verb" error. I am not able to figure out what I am missing.


Solution

  • Remark about WMIC : Don't forget that the output of WMIC is unicode !

    The trailing <CR> can be removed by passing the value through another FOR /F loop. This also removes the phantom "blank" line (actually a <CR>)

    So i tested this batch with "APPNAME=VirtualDJ 8" on my side and it works 5/5

    @echo off
    Title Get IdentifyingNumber from Application using WMIC
    set "APPNAME=VirtualDJ 8"
    set "GUID="
    echo    Please Wait a while ... Getting IdentifyingNumber from this Application "%APPNAME%"
    @for /f "skip=1 delims=" %%a in ('"wmic product where name="%APPNAME%" get IdentifyingNumber"') do (
        @for /f "delims=" %%b in ("%%a") do if not defined GUID set "GUID=%%~nb"
    )
    cls
    echo The Application "%AppName%" has a Guid like this one : "%GUID%"
    pause