Using wmic to Find if Product Exists

13,457

Choose any:

Read how FINDSTR will set ERRORLEVEL

@ECHO OFF
SETLOCAL EnableExtensions
set "_product=abc123"
rem set "_product=avg zen"

echo 'redirection' way
(wmic product get name| findstr /i /C:"%_product%")&&(
    echo %_product% exists
    rem uninstall here
  )||(
    echo %_product% no instance
  )

echo 'if errorlevel' way
wmic product get name| findstr /i /C:"%_product%"
if errorlevel 1 (
  echo %_product% no instance
) else (
  echo %_product% exists
  rem uninstall here
)

echo 'direct call' way
wmic product where "name='%_product%'" call uninstall/nointeractive

Output for set "_product=abc123":

==> D:\bat\SU\1087355.bat
'redirection' way
abc123 no instance
'if errorlevel' way
abc123 no instance
'direct call' way
No Instance(s) Available.

Output for set "_product=avg zen" but with 'direct call' way skipped:

==> D:\bat\SU\1087355.bat
'redirection' way
AVG Zen
avg zen exists
'if errorlevel' way
AVG Zen
avg zen exists
Share:
13,457

Related videos on Youtube

Samuel Pauk
Author by

Samuel Pauk

Updated on September 18, 2022

Comments

  • Samuel Pauk
    Samuel Pauk over 1 year

    I'm looking to write a batch file that checks to see if a program exists, and if it does exist I'd like to have it uninstalled. This is what I've got so far.

     @echo off
     (wmic product get name| findstr /i "abc123")
    

    It's not much, but basically if it finds "abc123" I'd like it to run run an uninstall on it. This is what I've got for this so far.

     wmic product where name="abc123" call uninstall/nointeractive
    

    I'm not sure how to set an 'if true' type of statement for the first set of code that activates the second set of code.

    Anything that comes back as 'false', the program basically skips the uninstall.

    If you have any questions please don't hesitate to ask. Thanks!

  • Mike 'Pomax' Kamermans
    Mike 'Pomax' Kamermans over 6 years
    Is there no direct way to ask wmic for all products it knows of, based on a substring match? Doing a full list on a computer that's been in use for a year or two takes quite a while, so if there's a way to avoid having it first run through the entire list of installed programs, that would easily cut out a 30 second wait.