taskkill window with space and quotation on its name? - windows

How do to kill a cmd window with space and quotation on its name?
I want to kill a window that its name is: Remote /C comupterName "session1"
taskkill /f /fi "WindowTiltle eq Remote /C comupterName "session1""
doesn't work, it don't find the window
Thanks

Solved, juste add double quotation:
taskkill /f /fi "WindowTiltle eq Remote /C comupterName ""session1"""

Related

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?

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.

Windows TASKKILL syntax issue

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*"

How to close all active windows/applications using a batch file

I am a beginner.I just went curious about cmd so I want to make a batch file that kills the active windows and shutdown/restart the computer safely.
I came across commands like-
taskkill /im "program.exe"
tasklist
shutdown -s
But I want to close all active windows but not forcefully.
If there a specific command or some combination of commands please do mention.
Thanks in Advance.
PS- I came across powershell but I want to know if i can achieve this using batch file (cmd commands) .Below is the link
How to close all windows
If you perform a treekill on explorer.exe, it will close all other programs except background processes. Those batch scripts will only work if they are called in an exceptional manner that makes them background processes, system processes or if they are not a child process of explorer.exe.
Here's the fastest reference implementation of my treekill explorer method
#echo off
echo closing all programs...
taskkill /f /t /im explorer.exe
explorer.exe
Here's an example implementation of my treekill explorer method combined with hibernate to make a fast shutdown and startup script.
#echo off
echo shutting down...
echo closing all programs...
taskkill /f /t /im explorer.exe
echo hibernating...
shutdown /f /h
echo restoring...
explorer.exe
echo thanks you for using JessieTessie's fast shutdown and startup.
You can do
#echo off
#powershell Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | stop-process
taskkill /f /im explorer.exe
It will execute powershell command that will find and close all running programs that isn't hidden or evelated using windowtitle.
But it will close all apps including yours.
To prevent that you need to recode it from c++ (you can use system("somecommand") from windows.h) and before executing closeAll commands put freeconsole() in code. But you will need to find how to get console back.
title Kill all running apps
cd c:\windows\System32
for /f "skip=3 tokens=1" %%i in ('TASKLIST /FI "USERNAME eq %userdomain%\%username%" /FI "STATUS eq running"') do (
if not "%%i"=="svchost.exe" (
if not "%%i"=="explorer.exe" (
if not "%%i"=="cmd.exe" (
if not "%%i"=="tasklist.exe" (
echo.
taskkill /f /im "%%i"
taskkill /f /im explorer.exe
echo.
)
)
)
)
)
pause

Killing a process from a new command prompt in a batch file

I have this sitting in a batch file, and I'm wondering why it's throwing
"The system cannot find the file TASKLIST"
FOR /F "usebackq tokens=2 skip=2" %%i IN (TASKLIST /FI "IMAGENAME eq explorer.exe") DO taskkill /F /PID %%i
More importantly, the problem is that that command - killing explorer.exe - has to run in the cmd spawned by the command
at xx:xx /interactive "cmd.exe"
How would I pipe that taskkill command into the new command prompt that would be spawned by the at command?
Thank you for your time.
if you want to process a exit of a command use single qutoes or back quotes with "backq" parameter:
FOR /F "tokens=2 skip=2" %%i IN ('TASKLIST /FI ^"IMAGENAME eq explorer.exe^"') DO taskkill /F /PID %%i
(im not sure if your backquotes are displayed because they meta-symbol for code block in stackoverflow)
Taskkill also can kill a process by image name
taskkill /im explorer* /f

Resources