Taskkill If - Else
Taskkill If - Else
use an `if` statement to conditionally execute the `taskkill` command. Here's how
you can do it in a batch script:
```batch
@echo off
tasklist /FI "IMAGENAME eq ms-teams.exe" 2>NUL | find /I /N "ms-teams.exe">NUL
if "%ERRORLEVEL%"=="0" (
echo ms-teams.exe is running, initiating taskkill...
taskkill /f /im ms-teams.exe
) else (
echo ms-teams.exe is not running.
)
This script first checks if `ms-teams.exe` and `msteams.exe` are running. If they
are, it initiates `taskkill`. If not, it simply prints a message saying that the
process is not running. The `2>NUL` part is used to suppress error messages from
the `tasklist` command. The `find /I /N` part is used to search for the process
name in the output of the `tasklist` command. The `if "%ERRORLEVEL%"=="0"` part
checks the exit status of the `find` command. If the exit status is 0, it means the
process was found (i.e., it is running). Otherwise, it means the process was not
found (i.e., it is not running).