x64 x86 exe, dynamic registry key, UninstallString with quotes,
32-bit version can be used on 64-bit windows

This method applies to Firefox

silent-install.cmd
@echo off
set sw=HKLM\SOFTWARE
set u=Microsoft\Windows\CurrentVersion\Uninstall
set n=Mozilla Firefox
if not "%ProgramFiles(x86)%"=="" set x=Wow6432Node\
setlocal EnableDelayedExpansion

:detect32bit
reg query "%sw%\%x%%u%" /s | find "%n%" > nul 2>&1
if !errorlevel!==0 goto exists

:nativeDetect
reg query "%sw%\%u%" /s | find "%n%" > nul 2>&1
if !errorlevel!==0 goto exists

echo %n%

:x64
if not "%ProgramFiles(x86)%"=="" (
for /f "delims=" %%a in ('dir /b "%~dp0Firefox*Setup*x64.exe"') do (
start /wait "" "%~dp0%%a" /SILENT
)
)

:x86
if "%ProgramFiles(x86)%"=="" (
for /f "delims=" %%a in ('dir /b "%~dp0Firefox*Setup*x86.exe"') do (
start /wait "" "%~dp0%%a" /SILENT
)
)

:exists
endlocal

silent-uninstall.cmd
@echo off
set sw=HKLM\SOFTWARE
set u=Microsoft\Windows\CurrentVersion\Uninstall
set n=Mozilla Firefox

if "%ProgramFiles(x86)%"=="" goto native

:only64bit
for /f "tokens=8 delims=\" %%a in ('
reg query "%sw%\Wow6432Node\%u%" /s ^|
find "%n%" ^|
find "HKEY_LOCAL_MACHINE"') do if not "%%a"=="" (
for /f delims^=^"^ tokens^=2 %%b in ('^
reg query "%sw%\Wow6432Node\%u%\%%a" /v UninstallString ^|
find "UninstallString"') do (
echo %%a found!
echo Removing 32-bit %n% from 64-bit system..
"%%b" /SILENT
setlocal EnableDelayedExpansion
:reCheck32bit
reg query "%sw%\Wow6432Node\%u%" /s | find "%n%" > nul 2>&1
if !errorlevel!==0 goto reCheck32bit
endlocal
)
)

:native
for /f "tokens=7 delims=\" %%a in ('
reg query "%sw%\%u%" /s ^|
find "%n%" ^|
find "HKEY_LOCAL_MACHINE"') do if not "%%a"=="" (
for /f delims^=^"^ tokens^=2 %%b in ('^
reg query "%sw%\%u%\%%a" /v UninstallString ^|
find "UninstallString"') do (
echo %%a found!
if "%ProgramFiles(x86)%"=="" echo Removing 32-bit %n% from 32-bit system..
if not "%ProgramFiles(x86)%"=="" echo Removing 64-bit %n% from 64-bit system..
"%%b" /SILENT
setlocal EnableDelayedExpansion
:reCheckNative
reg query "%sw%\%u%" /s | find "%n%" > nul 2>&1
if !errorlevel!==0 goto reCheckNative
endlocal
)
)

Comments