Windows TASKKILL syntax issue - windows

After all these years, you'd think I had done some Windows batch files of some type. But you'd be wrong. I am updating a batch file and encountered the following statement:
TASKKILL /F /FI "WINDOWTITLE eq SIM RH*" /IM perl.exe
Why both the /FI and the /IM? Are the two clauses anded together? If I leave off the /IM, the correct processes get killed.
The only way this makes sense is if they get ended and kill only tasks with that window title AND are perl.exe tasks.

Yes they are added!
/IM would be killing all tasks with different process-IDs
/FI applies a filter to that. You can test that with something like this:
#echo off
for /L %%i in (1,1,5) do start "%%i" cmd.exe
timeout /t 3
taskkill /F /FI "WindowName ne 3" /IM cmd.exe
This should leave you with 1 command-prompt with the title 3. All others will be closed.
Changing the ne to eq would only close the command-prompt with the title 3 leaving you with 1, 2, 4, 5 and yourBatch.bat

Yes. The given /FI "WINDOWTITLE eq SIM RH*" option will filter processes by window title starting with SIM RH. The reference for taskkill specifies that if such a filter is given, the wildcard character * is accepted for the /IM option.
What isn't being said is the fact that adding a filter and leaving out the /IM seems to imply /IM *.
You can verify this behaviour with following command, which will open a regular shell and a powershell in two separate windows, and then kill both windows just by specifying a window title filter.
start "testp1" cmd && start "testp2" powershell && taskkill /F /FI "WINDOWTITLE eq testp*"

Related

How to keep batch file running whilst using TASKKILL /f /t /im explorer.exe?

In short, I need to start 1.bat, that can CALL or START 2.bat to execute TASKKILL /im explorer.exe /f /t but keeps "1.bat" running (or reopens it) once "2.bat" is finished. The difficulty is, I need to keep the /t switch in TASKKILL to make it transferable.
Full Explanation:
I have several older laptops running Windows 7 x64. They will run games, but only if I use Task Manager to end Explorer, it's associated processes and stop several unneeded services. This frees up RAM and CPU to start games via Task Manager..
To avoid ending each process/service individually every time, I wrote 2 cmd batch files:
1) Options.bat -- (SHORTENED)
#echo off
:begin
echo (0) Kill Processes
echo (1) Run [game]
SET /p op=Select Task:
if "%op%"=="0" goto killall
if "%op%"=="1" goto op1
killall:
CALL "C:\TK.bat"
cls
goto begin
op1:
start " " /realtime "C:\[exe path]"
goto exit
:exit
exit
2) TK.bat -- (SHORTENED)
net stop [service]
taskkill /im explorer.exe /f /t
taskkill /im [specific process].exe /f /t
Both of which work as intended - provided I initially start Options.bat via Task Manager.
Problem: when I run Options.bat from Windows Explorer (even "Run As Administrator") and call the TK.bat script to run TASKKILL /im explorer.exe /f /t it does work, but also closes the CMD window, when i want it to return to the options selection. This does make sense when i'm using the "tree" attribute for TASKKILL.
However, if I run Options.bat via Task Manager, and CALL TK.bat, it will execute the commands (without closing itself) then return to the task selection - which is exactly what i want to happen!
I assume this is because it is running as the Local System account via Task Manager and not mine or the built in Administrator account?
I have tried the RUNAS command within Options.bat -
RUNAS /user:Adminstrator "C:\TK.bat"
which runs TK.bat, executes the TASKKILL command and then RUNAS again to return to the first batch file in my username. This does work, but I still have to press enter at each password prompt.
I have also tried numerous variations of the TASKKILL /FI switch:
TASKKILL /fi IMAGENAME ne cmd.exe /im explorer.exe /f /t`
TASKKILL /fi USERNAME eq [name] /fi WINDOWTITLE ne Options.bat /im explorer.exe /f /t
Which, I thought, would end all processes "not equal" to cmd.exe/Options.bat but I cannot get it to work.
Question: Is there a way of executing the TASKKILL /im explorer.exe /f /t within TK.bat, that will not close the currently running batch file and without having to run it from Task Manager? Perhaps a different command or giving the Options.bat some sort of elevated authority to stop it from being closed when Explorer.exe /t is ended. Ideally, without installing separate 3rd party tools?
I know I could edit the batch file to end explorer.exe, end each associated .exe and then stop the services I do not need, individually, but this is time consuming and defeats the point of writing the file to make it automated and transferable (if needed).
Any help would be greatly appreciated and I can upload the full code of both batch files, if that would help.
Strangely enough, the simple taskkill /F /IM explorer.exe & start explorer command does this task. I don't know why, but it works.
To prevent taskkill /im explorer.exe /f /t from killing the cmd instance which is executing your batch file, You need to break the parent-child relationship between that particular instance of cmd.exe and explorer.exe.
The trick is to use two extra instances of nested cmds. The first one launches the second one and terminates immediately so the second instance becomes an orphaned process which can not be determined as descendant of exeplorer.exe. At this point the second instance can safely execute taskkill /im explorer.exe /f /t
start "Chain breaker" /min cmd /d /c start "Orphaned Process" cmd /d /c Options.bat
The above can be used from Command Prompt or from another batch file.
You can also incorporate this technique directly in to the Options.bat file, But extra logic is needed for Options.bat to determine when to launch itself in nested cmd and when to execute the actual code.
A sample script demonstrating the concept would be:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=3 delims=:" %%L in ("%~0") do goto %%L
REM Reinvoke the batch file forcing it to jump to the :main label
start "Chain breaker" /min cmd /d /c start "Orphaned Process" cmd /d /c #"%~d0\:main:\..%~pnx0"
exit /b
:main
taskkill /im explorer.exe /f /t
echo I'm alive
pause
exit /b

unable to find .bat pid for killing

I am unable to call a bat process within another bat.
This is the situation.
I have a n1.bat that basically contains:
TITLE "n1.bat"
...
...
start /b /MIN "n2.bat"
...
The n2.bat contains:
TITLE "n2.bat"
...
...
start someother.bat
start another.bat
exit
Now I use a third bat (n3.bat) that must kill everything.
TITLE "n3.bat"
...
TASKKILL /F /T /PID n1_PID
...
exit 0
Unfortunately when n3.bat ends a window called n1.bat - n2.bat remains active (the echo output belongs to n2.bat).
I tried to kill it in some ways:
1- Tried to get the process pid from tasklist -> there is no process called n2.bat or similar
TASKLIST /V /NH>Tasks.txt
FOR /F "tokens=2 delims= " %%n2_pid IN ('FINDSTR n2.bat Tasks.txt') DO SET PID=%%n2_pid
TASKKILL /PID %n2_pid%
2- Trying to use TASKKILL /F /FI "WINDOWTITLE eq n2.bat" -> no process found
I tried the previous solutions also starting n2.bat with
start /min "n2.bat" (so without /b)
with no success.
The only way I manage to kill it is to calling
TASKKILL /F /IM cmd.exe
that I really would like to avoid since it obviously kills all opened cmd.
Any ideas to retrieve the guilty pid?

Batch script - close Cmd.exe window and restart

I have a batch file script which starts a program (bat) and after X seconds the batch script is close the program (exe) and restart it.
This is my code:
:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
goto loop
If the program is ending, I want that the Cmd.exe window closes, but only the "1.bat" window should be closed. I can't find a specified cmd window for my "1.bat" process in the task manager to close it.
If I close the programm.exe with && exit the batch doesn't restart!
You can do one of two things.
1 - If you have access to 1.bat (i.e. its not readonly or is not actually an .exe), then add exit after it runs the program.exe.
2 - Otherwise if you cannot modify 1.bat for whatever reason, you may need to add the following command line:
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"
So your batch script will look as follows:
:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"
goto loop
Or similar. Basically /FI is the filter switch and you can specify to filter based on the "IMAGENAME" (which equals "cmd.exe") and "WINDOWTITLE" (which equals "1.bat")

How to close cmd in batch file

start cmd /k
/k: is compulsory which will execute.
launching many command propmts can be done as below.
start cmd /k Call rc_hub.bat 4444
start cmd /k Call rc_grid1.bat 5555
start cmd /k Call rc_grid1.bat 6666
start cmd /k Call rc_grid1.bat 5570.
I want to know what cmd to be used for closing after launching
Eg: if %a="1234" start cmd /k Call rc_grid1.bat --> This is opening cmd , what cmd needs to give to close ?
Closing multiple instances of cmd is not a very uncommon task.
It can arise, for example, when debugging some complex set of batch scripts.
There is one of the ways to do this:
First, give cmd windows unique titles. For example,
start "unique_title" cmd.exe /k ...
Second, when you want to close them, get process ids from tasklist output matching the titles. Then kill those ids with taskkill.
tasklist /v /fo csv | findstr "unique_title"
Here is the full example, the first argument is the title substring to match:
kill_title.cmd
#echo off
set TITLE=unique_title
if not "%~1"=="" set "TITLE=%~1"
for /f "tokens=1,2,* delims=," %%a in ('tasklist /fi "imagename eq cmd.exe" /v /fo csv ^| findstr "%TITLE%"') do (
rem here we check that the first column is "cmd.exe" ... just in case
if "%%~a"=="cmd.exe" echo taskkill /pid "%%~b"
)
First check the script output, and if it is ok, remove echo before taskkill.
When messing with command prompts, I kept crashing my computer when command prompts got REALLY out of control. I made another command prompt as follows:
Title END
:End
taskkill /FI "WINDOWTITLE ne END" /IM cmd.exe /F /T
Goto :End
This will force close all command prompts that are running, not including this one, and will repeat the command until you close it.

Writing a batch-file to kill process: f.lux when opening color sensitive work

I'm trying to produce a batch file that can check to see if a process is running such as a game, photoshop, or media and terminate a specific process. In this case 'f.lux' considering it dims the screen and can affect color quality. I've looked for some answers on here for this with no luck, as most of the content is older and I'm not sure how much has changed in Windows 10. So far here is what I have picked up off of some other posts.
tasklist /FI "IMAGENAME eq example_process.exe" 2>NUL | find /I /N "example_process.exe">NUL
if "%ERRORLEVEL%"=="0" taskkill /im f.lux.exe
Not sure how correct this is, so any advice or help would be appreciated.
Thanks.
taskkill /im f.lux.exe
either this or
taskkill /f /im f.lux.exe
See taskkill/?
Just change to your task to be killed instead of iexplore.exe task in this example :
#echo off
set task=iexplore.exe
set Active_task=0
for /f %%q In ('tasklist') DO (if /i "%%q" EQU "%task%" set Active_task=1)
if %Active_task% EQU 1 (
Taskkill /PID %task% /F
)
Pause

Resources