I've been struggling to get a specific output using WMIC in batch in order to build an automatic uninstall script. The issue I'm running across is that the uninstaller for the application I'm trying to remove is created under an auto-generated SSID on each system (e.g.: C:\ProgramData{07BFF8FA-C12F-46C7-8239-8EE83E21B5DA}\program-name\Uninstall.exe). Because of this, I can't build a static uninstall location based on the registry as the uninstaller string also is under the same SSID in the registry.
I've tried a couple of different ways of pulling the uninstall info and the only one I've landed on that's come close is using WMIC:
wmic product where "Name like '%product name%'" get name
which outputs:
Name
<product-name>
^ and an extra carriage return, and that carriage return is the issue. It sets the variable, 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 %PROD% variable isn't defined at all.
If I run the batch with @echo ON, I get this:
:\Users\Administrator>echo <product-name>
<product-name>
:\Users\Administrator>echo
ECHO is on.
Notice the output is missing the drive letter. That's exactly what I'm seeing so it's weird, and also the parameter is being set, echo'd then unset.
I've 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 this:
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 correctly outputs the product name
However, when I run it from batch as:
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
I get instead:
No Instance(s) Available.
Which to me sounds like an issue WMIC is having with syntax or something
SOLVED!
Credit to npocmaka for suggesting a nested FOR loop, and indiv for pointing out the escape logic for the WMIC variable
Correct syntax of the command used in batch:
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 a ton guys!
EDIT.Turns out that %
is needed to be used as 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%
it is explained here