Get product name from WMIC for variable in batch
I am trying to get specific output using WMIC in batch in order to create an automatic uninstall script. The problem I'm running into is that the uninstaller for the app I'm trying to uninstall is created under an auto-generated SSID on every system (ex: C: \ ProgramData {07BFF8FA-C12F-46C7-8239-8EE83E21B5DA} \ program name \ Uninstall.exe). Because of this, I am unable to create a registry based static uninstallation location as the uninstaller line is also under the same SSID in the registry.
I've tried several different ways to pull in the delete information and only the one I landed with comes close uses WMIC:
wmic product where "Name like '%product name%'" get name
which outputs:
Name
<product-name>
^ and an optional carriage return, and this is a carriage return. It sets a variable and then clears it.
Here's the for loop I'm trying to use to get this to work:
@echo off
for /f "skip=1 delims==" %%a in (
'wmic product where "Name like '%product-name%' get name'
) do set PROD=%%a
echo %PROD%
which outputs:
C:\Users\Administrator>ECHO is off.
which means the variable% PROD% is not defined at all.
If I run the package with @echo ON, I get this:
:\Users\Administrator>echo <product-name>
<product-name>
:\Users\Administrator>echo
ECHO is on.
Note that the output is missing a drive letter. This is what I see, so it's weird and also the parameter is set, then the echo is canceled.
I also tried to do this via a text file relay:
wmic /OUTPUT:%~dp0\wmic.txt product where "Name like '%product-name%'" get name
for /f %%a in (
"%~dp0\wmic.txt" | findstr /v "product-name"
) do set PROD=%%a
Any help / advice would be most welcome!
UPDATE!
following link provided by npocmaka, I came up with the following:
for /f "skip=1 delims=" %a in ('wmic product where "Name like '%product-name%'" get name') do @for /f "delims=" %b in ("%a") do @echo %b
which outputs the product name correctly
However, when I run it from a package like:
for /f "skip=1 delims=" %%a in (
'wmic product where "Name like '%product-name%'" get name'
) do @for /f "delims=" %%b in ("%%a") do echo %%b
Instead, I get:
No Instance(s) Available.
Which sounds like a problem to me that WMIC has syntax or something
DECIDE!
Credit to npocmaka for suggesting a nested FOR and indiv loop for specifying the exit logic for a WMIC variable
The correct syntax for the command used in the package is:
for /f "skip=1 delims=" %%a in (
'wmic product where "Name like '%%product-name%%'" get name'
) do @for /f "delims=" %%b in ("%%a") do @echo %%b
Thanks tons of guys!
source to share
EDIT . It turns out that %
you need to use as a wildcard
@echo off
for /f "skip=1 delims==" %%a in (
'wmic product where "Name like '%%product-name%%'" get name /format:table'
) do (
for /f "tokens=* delims=" %%# in ("%%a") do set PROD=%%a
)
echo %PROD%
here
source to share