title: Get product name from WMIC for a variable in batch
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>wmic product where "Name like '%product name%'" get name
  </code></pre>
  
  <p>which outputs:</p>
  
  <pre><code>Name
  &lt;product-name&gt;
  </code></pre>
  
  <p>^ and an extra carriage return, and that carriage return is the issue. It sets the variable, then clears it.</p>
  
  <p>Here's the for loop I'm trying to use to get this to work:</p>
  
  <pre><code>@echo off
  for /f "skip=1 delims==" %%a in (
       'wmic product where "Name like '%product-name%' get name'
  ) do set PROD=%%a
  echo %PROD%
  </code></pre>
  
  <p>which outputs:</p>
  
  <pre><code>C:\Users\Administrator&gt;ECHO is off.
  </code></pre>
  
  <p>which means the %PROD% variable isn't defined at all.</p>
  
  <p>If I run the batch with @echo ON, I get this:</p>
  
  <pre><code>:\Users\Administrator&gt;echo &lt;product-name&gt;
  &lt;product-name&gt;
  :\Users\Administrator&gt;echo
  ECHO is on.
  </code></pre>
  
  <p>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. </p>
  
  <p>I've also tried to do this via a text file relay:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Any help/advice would be most welcome!</p>
  
  <hr>
  
  <p>UPDATE!</p>
  
  <p>following link provided by npocmaka, I came up with this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>which correctly outputs the product name</p>
  
  <p>However, when I run it from batch as:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I get instead:</p>
  
  <pre><code>No Instance(s) Available.
  </code></pre>
  
  <p>Which to me sounds like an issue WMIC is having with syntax or something</p>
  
  <hr>
  
  <p>SOLVED!</p>
  
  <p>Credit to npocmaka for suggesting a nested FOR loop, and indiv for pointing out the escape logic for the WMIC variable</p>
  
  <p>Correct syntax of the command used in batch:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Thanks a ton guys!</p>