@ECHO OFF :: =========================================================================== :: This batch file closes all running instances of a given. :: executable, gracefully at first, then more forcibly. It :: is intended for use with AlwaysUp :: (http://www.coretechnologies.com/products/AlwaysUp/) :: :: Usage: :: close-application :: where :: is the name of the executable to be closed. :: :: Created by Core Technologies Consulting, LLC :: https://www.coretechnologies.com/ :: =========================================================================== :: The name of the executable to be closed, passed as the :: first argument. IF [%1] == [] GOTO show-usage SET exe-name=%1 :: Done if the executable is not running tasklist /nh /fi "imagename eq %exe-name%" | find /i "%exe-name%" IF %ERRORLEVEL% EQU 1 GOTO not-running :: It's running. Try to stop it, gracefully. taskkill /im "%exe-name%" :: Wait 10 seconds for it to go. ping 1.1.1.1 -n 1 -w 10000 > nul tasklist /nh /fi "imagename eq %exe-name%" | find /i "%exe-name%" IF %ERRORLEVEL% EQU 1 GOTO closed-gracefully :: Try again, gracefully. taskkill /im "%exe-name%" :: Wait another 10 seconds for it to go peacefully. ping 1.1.1.1 -n 1 -w 10000 > nul tasklist /nh /fi "imagename eq %exe-name%" | find /i "%exe-name%" IF %ERRORLEVEL% EQU 1 GOTO closed-gracefully :: OK, it still hasn't closed gracefully. Be more forceful. taskkill /f /im "%exe-name%" :: Wait 2 seconds for it to go. ping 1.1.1.1 -n 1 -w 2000 > nul tasklist /nh /fi "imagename eq %exe-name%" | find /i "%exe-name%" IF %ERRORLEVEL% EQU 1 GOTO closed-forcibly :: It's still running! Fail. echo Failed to close %exe-name%! exit 1 :show-usage echo Usage: close-application ^ exit 0 :closed-forcibly echo %exe-name% was closed forcibly. exit 0 :closed-gracefully echo %exe-name% was closed gracefully. exit 0 :not-running echo %exe-name% was not running. exit 0