Batch file that closes all programs except certain ones

Viewed 287

I'm trying to write a batch file (or powershell script) that closes all programs with active windows, except certain programs. I also don't want just any program Windows is running to be closed, just programs I've opened. I also can't program a list of what programs tokill, so I can't simply do this:

@echo off
set programs[0] = program1;
set programs[1] = program2; etc;

for %%a in (%programs%) do (
  taskkill /F /im %%a
  echo/
)

I would like something that looks like this (<...> indicating parts I don't know):

@echo off
set safeList[0] = program1;
set safeList[1] = program2; etc;

set activePrograms = <get all active programs that aren't basic windows processes, startup programs, etc.>

for %%a in (%activePrograms%) do (
  if(<%%a not found in safeList>) (
    taskkill /F /im %%a
    echo/
  )
)

Obviously, the activePrograms part should not include basic windows tasks (ie. explorer.exe), as that would be chaotic. I would like to not have to define every single running task in safeList.

3 Answers

if you just want to close the programs that you open. which is shown in task manager as App enter image description here

for this list, you can use the get-process command as follow:

Get-Process | Where-Object {$_.MainWindowTitle -and $_.Description -and $_.Name -ne "ApplicationFrameHost"}

and in order to terminate this apps you can pipe the output to the command Stop-Process as follow:

 Get-Process | Where-Object {$_.MainWindowTitle -and $_.Description -and $_.Name -ne "ApplicationFrameHost"}  | Stop-Process

Finally, i should flag as mentioned by @IRon that killing programs might lead to data loss and you should test this method on every windows version before applying it. in order not to close unneeded apps.

This script should terminate all programs runing under your username and NOT runing as windows service. UNTESTED

for /f tokens=2 %a in ('tasklist /V ^| findstr /I /C:"%COMPUTERNAME%\%USERNAME%" /C:"Console"') do taskkill /PID %a

according to iRon`s comment, you should exclude some system needed programs like explorer.exe

You can try with powershell


Get-Process | ? {$_.MainWindowTitle -ne ''} | Stop-Process

With a Batch file :

@echo off
Title Kill and closes all programs except certain ones
Powershell ^
Get-Process ^| ? {$_.MainWindowTitle -ne ''} ^| Stop-Process

Or you can try with this batch too :

Close All Program Except these with CMD

Related