First, you have the wrong command to stop the process, for example calc.exe.NET STOP will stop the service, but not the normal program process. Instead, you want TASKKILL.
The second one. If you know you want to kill a process, if it exists, then why do you think you need to check if it is executed first before you kill it? You can simply try to kill the process independently. If it does not exist, an error is generated and no harm is done. If it exists, then it will be killed with a successful message.
taskkill /im calc.exe
If you do not want to see the error message, redirect stderr to nul using 2>nul . If you also do not want to see a success message, then redirect stdout to nul using >nul .
taskkill /im calc.exe >nul 2>nul
If you want to take action depending on the result, you can use conditional && and || operators for success and failure.
taskkill /im calc.exe >nul 2>nul && ( echo calc.exe was killed ) || ( echo no process was killed )
Or you can use ERRORLEVEL to determine what to do based on the result.
source share