Pre-Installation Script

Quite often, when you want to upgrade an application there is a risk that the old version is currently running on the system.
For example, when you want to upgrade Microsoft Office 2007 to Office 2010, there is a risk that the user is using Outlook, Word, Excel or PowerPoint. Which, will prevent the Office setup from replacing and upgrading those files and applications.

Sooner or later, the installation will go through when those apps are not running, or if you choose during computer startup. Though, until that happens, you may see the Office setup reporting Error in the installation.

It could also be nice to be able to kill the process if it’s running and thus make sure the app is not running and is able to be upgraded.

Here is a small script you can use and modify to either just detect if a process is running, and then wait with the upgrade. Or, kill the process and then if successful let the setup continue.

Just save this script in the same folder as your .msi file or setup program.

@Echo off

set force=no

IF /I [%1] EQU [] goto syntax
IF /I [%1] NEQ [] set process=%1
IF /I [%2] EQU [force] set force=yes

Echo Proecess Name to look for      : %process%
Echo Should %1 be killed if running : %force%

if %force% == yes goto kill
goto detect

echo Look for %process% and kill it if running 
tasklist /FI "Imagename eq %process%" | findstr /i /c:%process%
if %errorlevel% == 1 exit /b 0
taskkill /T /F /IM %process% 
goto detect

echo Start Detect
tasklist /FI "Imagename eq %process%" | findstr /i /c:%process%
echo %errorlevel%
if %errorlevel% == 1 exit /b 0   
if %errorlevel% == 0 exit /b 1

goto end

Echo Syntax: 
Echo %~nx0 processname.exe [force]
Echo Processname is mandatory to inform the script which process to look for. 
Echo Force is optional, will kill the process if specified else just detects. 
Echo Exit codes,  0 = Process is not running 
Echo              1 = Process is running 

goto end


#End of script

And then use the Pre Installation Command feature.

In this example I’m trying to upgrade VLC and want to force an update even if the process is running.

I’ve used: %SpecopsDeployExecuteDir%prescript.bat
That means that the prescript.bat will be executed from the same folder as my setup process.

Parameters: vlc.exe force
Force means, search for the process vlc.exe and kill the process if it’s running.
If you only specify vlc.exe it will just be detected and not killed.

Don’t ignore the exit code, and leave it to 0.
If the process is not running, the script will exit with code 0 = Proceed with installation
If the process is running, the script will exit with code 1 = Don’t proceed with installation and do a new check next gpupdate.

The script example is currently accepting just one process. But it shouldn’t be too hard to expand it to cover for example; winword.exe, excel.exe, outlook.exe and powerpnt.exe at the same time.

(Last updated on October 30, 2023)

Back to Blog