Using msiexec in an ‘if exists’ login script to install an .msi

I wrote this script to handle a company wide Keepass update in my company.  The script checks to see if a file (with the name of the version of Keepass) exists.  If it does then it proceeds with the install, if not it closes the program.

The script also checks the location of the install.  Annoyingly Keepass installs in two locations depending on the installer you use.  I just used the license file in each path as a means to check if the path exists.  I could then force the installer to upgrade each path using the TARGETDIR= command.

Lastly the script will cleanup the registry so that old versions of Keepass don’t appear in Programs and Features.

set InstallRev=239

if exist "C:\Program Files (x86)\KeePass Password Safe 2\%InstallRev%.txt" goto Finish

if exist "C:\Program Files (x86)\KeePass2x\%InstallRev%.txt" goto Finish

if exist "C:\Program Files (x86)\KeePass Password Safe 2\License.txt" goto KP1

if exist "C:\Program Files (x86)\KeePass2x\License.txt" goto KP2

:KP1

msiexec /i \\server\share\folder\KeePass-2.39.msi TARGETDIR="C:\Program Files (x86)\KeePass Password Safe 2" /qn
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\KeePassPasswordSafe2_is1" /f

:KP1

goto Finish

:KP2

msiexec /i \\server\share\folder\KeePass-2.39.msi /qn

reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\KeePassPasswordSafe2_is1" /f

:KP2

goto Finish

:Finish

echo %InstallRev% > "C:\Program Files (x86)\KeePass Password Safe 2\%InstallRev%.txt"
goto Finish

:Finish

Exit

Leave a Reply

Your email address will not be published. Required fields are marked *