3
votes

I'm attempting to make a batch script to uninstall various programs with little involvement from the user (Only saying I want to uninstall Group A (Say, Google Chrome and Microsoft Office)), Partially for work and partially for fun and practice. This itself isn't impossible, I can already do this by calling msiexec and pointing it to the uninstall location in the registry (msiexec /x {xxxxx-xxxxx-xxxxxx-xxxx}).

The only problem with this is that this means that whenever the program changes registry keys, as will often happen with updates, I have to find the path again. So, I'm trying to run a piece of code that will search through the HKLM\SOFTWARE\Mincrosoft\Windows\CurrentVersion\Uninstall registry location, and whenever the DisplayName returns a certain value, not necessarily exact (Say "Chrome"), it stores the registry location?(The part that would be put in {xxxxxxx-xxxxxxx-xxxxxxx-xxxxxx}).

This way, the program simply finds the programs and stores their locations so that they can be uninstalled with it as a variable. I have tried, but as far as I know using reg query requires the full location. using reg query HKLM /f returns no results. I've tried a whole range of different workarounds, but nothing seems to work.

Here is a basic version of my code (I am neglecting to post the full version because much of it is repeated. It's mostly just case where's and if then statements)

@echo off
:start
echo Hello
echo.
1: Uninstall Chrome

set /p choice="Enter Choice: "
if "%choice%"=="1" goto uninstall_chrome

:uninstall_chrome
reg query HKLM /f Chrome /t REG_SZ
::msiexec /x{uninstall location for the program}

Thanks.

::UPDATE::

I figured it out. By using some of Rob van der Woude's code, I am now able to do it. The code to do so is:

CALL :Uninstall "Program Name"

:Uninstall
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=* %%A IN ('REG QUERY HKLM\SOFWARE\Microsoft\Windows\CurrentVersion\Uninstall /F "%~1" /D /S 2^>NUL ^| FINDSTR /R /B /C:"HKEY_"') DO (
REG QUERY "%%~A" /F DisplayName /V /E | FINDSTR /R /I /C:" DisplayName .* .*%~1" >NUL 2>&1
FOR /F "tokens=2*" %%B IN ('REG QUERY "%%~A" /F DisplayName /V /E 2^>NUL ^| FIND /I " DisplayName "') DO ECHO Program Name = %%C
FOR /F "tokens=7 delims=\" %%B IN ("%%~A") DO ECHO Unique Identifier = %%B
FOR /F "tokens=2*" %%B IN ('REG QUERY "%%~A" /F UninstallString /V /E ^| FIND /I " UninstallString "') DO %%C /qb
)
ENDLOCAL

This will output the programs name and unique identifier, then uninstall the program, without user input ("Do you want to uninstall x?")

1

1 Answers

1
votes

I figured it out. By using some of Rob van der Woude's code, I am now able to do it. The code to do so is:

CALL :Uninstall "Program Name"

:Uninstall
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=* %%A IN ('REG QUERY HKLM\SOFWARE\Microsoft\Windows\CurrentVersion\Uninstall /F "%~1" /D /S 2^>NUL ^| FINDSTR /R /B /C:"HKEY_"') DO (
REG QUERY "%%~A" /F DisplayName /V /E | FINDSTR /R /I /C:" DisplayName .* .*%~1" >NUL 2>&1
FOR /F "tokens=2*" %%B IN ('REG QUERY "%%~A" /F DisplayName /V /E 2^>NUL ^| FIND /I " DisplayName "') DO ECHO Program Name = %%C
FOR /F "tokens=7 delims=\" %%B IN ("%%~A") DO ECHO Unique Identifier = %%B
FOR /F "tokens=2*" %%B IN ('REG QUERY "%%~A" /F UninstallString /V /E ^| FIND /I " UninstallString "') DO %%C /qb
)
ENDLOCAL

This will output the programs name and unique identifier, then uninstall the program, without user input ("Do you want to uninstall x?")