0% found this document useful (0 votes)
5 views

Taskkill If - Else

Uploaded by

lmaxphl
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Taskkill If - Else

Uploaded by

lmaxphl
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Sure, you can use the `tasklist` command to check if a process is running and then

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.
)

tasklist /FI "IMAGENAME eq msteams.exe" 2>NUL | find /I /N "msteams.exe">NUL


if "%ERRORLEVEL%"=="0" (
echo msteams.exe is running, initiating taskkill...
taskkill /f /im msteams.exe
) else (
echo msteams.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).

You might also like