4

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.

Sam
  • 93
  • 5
  • `explorer.exe` is not only a "basic windows task" but also a file browser opened by the user. In other words, this is goes probably much deeper then you imagine. I don't think you can identify this list you looking for with a batch file and need at least PowerShell to define which programs connect to the open windows. Or just: **Press Ctrl-Alt-Delete and then Alt-T to open Task Manager's Applications tab. Press the down arrow, and then Shift-down arrow to select all the programs listed in the window. When they're all selected, press Alt-E, then Alt-F, and finally x to close Task Manager.** – iRon Jan 29 '22 at 17:44
  • 1
    It's dangerous to kill all but certain processes. Also I wouldn't use the force option `/F` as it shouldn't be necessary for normally running processes… – aschipfl Jan 29 '22 at 22:32
  • There is no obvious "*list of what programs to kill*" or "*basic windows tasks*", think of e.g. programs thate.g. autostart. Besides it is **a bad idea to just kill programs** without explicitly defining them (**this could lead to data loss**). I recommend you to simply log off and back on, which will go with sertain warnings (depending on the programs that can't be closed without risk) which also will shows you why you shouldn't automate this with a simple (batch) script. – iRon Jan 30 '22 at 09:18

3 Answers3

5

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.

Mahmoud Moawad
  • 697
  • 7
  • 14
  • What makes `ApplicationFrameHost` special? How can you make sure that aren't any other programs like this in other Windows versions, configurations or applications? Besides, I think you should give a warning that just ⚠️ **killing programs might lead to data loss**. – iRon Jan 30 '22 at 09:22
  • @IRon : Really appreciate you comment, and you are right about your concern i should give a warning about data loss of killing programs, but as @Sam requested: he need to close `programs he had opened` - so i tried first to list only the apps opened by users and not built in process and compare the result from the powershell and between the task manager list which i think he need to close, and then display the result, one difference was `ApplicationFrameHost` and after searching on it here https://www.file.net/process/applicationframehost.exe.html i though its a good idea to exclude it. – Mahmoud Moawad Jan 30 '22 at 09:44
  • 1
    @Iron : i modified the post to flag the data loss as you mentioned, thanks for your comment – Mahmoud Moawad Jan 30 '22 at 09:48
1

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

Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • So the batch file in the link you provided works (the one using WMIC), but it ignores entries in my whitelist. Is there a way of modifying the second batch file provided in the link to use PID values, Process Names, or something a little more reliable than the way it is currently written? – Sam Jan 29 '22 at 21:45
  • Or can you explain what criteria it is using to determine whether to terminate? If it would output this information (manufacturer?) to the "Terminated_List" I can do this by trial and error. Not sure how to tweak it to do this though. – Sam Jan 29 '22 at 21:47
  • So it is failing when the "manufacturer" field is blank. Is there a work around I can add to your code? Maybe have it check for process filenames as well? So instead of listing the manufacturer to exempt a file, you can add the process name or filename? I have no idea how to do this unfortunately, Batch is no something I'm proficient it. – Sam Jan 30 '22 at 01:03
0

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

user2956477
  • 1,208
  • 9
  • 17
  • 1
    How would I have it check the exclusion list ("safeList" as per my example above). – Sam Jan 29 '22 at 20:35