3

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

Community
  • 1
  • 1
xcepti0n
  • 41
  • 1
  • 1
  • 4
  • 1
    `Shutdown /s` will do a graceful shutdown, and `shutdown /s /f` will force close everything. If you can "close active windows but not forcefully" then `shutdown /s` will do it. If they refuse to close - e.g. Notepad prompting to save a file - then what would your script do differently to shutdown? – TessellatingHeckler Aug 03 '16 at 18:20
  • @TessellatingHeckler actually I want to save and close my applications- as i want to use this batch file when i am in hurry – xcepti0n Aug 03 '16 at 18:38
  • @TessellatingHeckler(continued from previous comment) if there's an option to do that otherwise it(shutdown /s) will work for me.Thanks for a nice reply i didn't knew it works like this. – xcepti0n Aug 03 '16 at 18:52
  • 1
    to ask an application to quit, Windows sends the application a [`WM_QUIT`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632641%28v=vs.85%29.aspx) message internally, and waits for the application to quit itself. To force-close it, Windows acts outside the application and ends the processes. I don't know of an internal message "save and quit" - every application has a different idea what "save" means - and Windows can't do the save because it doesn't know what the program is doing at all. I can't say it's impossible, but I don't *think* you can do it in any sensible general way. – TessellatingHeckler Aug 03 '16 at 19:11
  • for the "in hurry" scenario, maybe [shutdown](https://ss64.com/nt/shutdown.html) parameter `/h` is useful. on Win10 `shutdown /?` lists another parameter `/hybrid`, which looks promising. – Stephan Oct 11 '17 at 08:54

3 Answers3

5

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.
Jessie Lesbian
  • 1,273
  • 10
  • 14
0

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.

CALISVALIS1010
  • 157
  • 1
  • 1
  • 7
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Anatolii Apr 15 '21 at 09:01
-1
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
  • 1
    Add some explanations in answer – ray Jan 21 '19 at 13:47
  • This code closed my apps, then (ironically) got stuck on itself with a dialog saying 'Kill all running apps didn't start properly, do you want to close.' or something to that effect. – Kirk Ross Feb 22 '19 at 19:04