0

I'm trying to create a basic script to uninstall an application across all our endpoints using cmd;

msiexec /quiet /norestart /uninstall {xxxx-xxx-xxx-xxxx-xxxxx}

Due to different versions, the same app may have multiple GUIDs on different endpoints

How can I run the wmic product get name,IdentifyingNumber cmdlet to search for a specific application and set it's GUID also to a variable?

wmic product get name,IdentifyingNumber"
IdentifyingNumber                       Name
{E8CAD3B5-7016-45AE-97DF-098B5C8D4AC8}   App1
{90160000-008C-0000-1000-0000000FF1CE}   App

I can find and match the Application to a variable, bit is struggling setting the GUID to a variable.

FOR /F "tokens=2 delims==" %%A IN ('WMIC product GET Name /VALUE ^| FIND /I "App1"') DO SET _application=%%A
ECHO Application: "%_application%"
Rem ECHO GUID:%_GUIDVALUE% //Matching Application GUID

Any help would appreciated

Stephan
  • 53,940
  • 10
  • 58
  • 91
Richard
  • 3
  • 2
  • Well, in your code you are just querying `Name`. Anyway, instead of `find` you could use the following `wmic` command line: `wmic Product where "Name like 'App1'" get IdentifyingNumber,Name /VALUE`; when capturing its output with `for /F` ensure to escape the unquoted comma like `^,`. To avoid nasty Unicode-to-ASCII/ANSI conversion artefacts consult [this post](https://stackoverflow.com/a/53555500)… – aschipfl Jun 17 '20 at 18:40

2 Answers2

0

A FOR /F loop can retrieve the GUID ID. Change the value of APPNAME to what you are seeking. How are you planning to hadle the case of multiple versions of an app being installed on the machine?

@ECHO OFF
SET "APPNAME=Windows SDK"
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
    "Get-CimInstance -Class Win32_Product |" ^
    "Where-Object { $_.Name -eq '%APPNAME%' } |" ^
    "ForEach-Object { $_.IdentifyingNumber }"') DO (SET "APPID=%%A")
ECHO APPID is %APPID%
lit
  • 14,456
  • 10
  • 65
  • 119
0

wmic has a few ugly habits that makes its output hard to parse *), but it's not impossible:

@echo off 
setlocal

for /f %%a in ('wmic product where "name='Update for Windows 10 for x64-based Systems (KB4023057)'" get name^,IdentifyingNumber^|find "{"') do set "GUID=%%a"

echo %GUID%

*) especially
1. an ugly line ending of CRCRLF, which doesn't apply here, because we don't use a string "at the end of a line".
2. Some "empty" lines (not really empty - they contain a remaining CR), which we overcome with find "{" (we know, that char will be in the desired line).

Stephan
  • 53,940
  • 10
  • 58
  • 91