Reputation: 53
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
Upvotes: 4
Views: 48041
Reputation: 333
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.
Upvotes: 0
Reputation: 1546
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.
Upvotes: 6
Reputation: 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
Upvotes: -1