Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 970
xcepti0n Avatar asked Oct 29 '25 05:10

xcepti0n


1 Answers

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.
like image 171
Jessie Lesbian Avatar answered Oct 31 '25 11:10

Jessie Lesbian