Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file that closes all programs except certain ones [closed]

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.

like image 615
Sam Avatar asked Oct 23 '25 00:10

Sam


2 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.

like image 177
Mahmoud Moawad Avatar answered Oct 25 '25 21:10

Mahmoud Moawad


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

like image 21
Hackoo Avatar answered Oct 25 '25 21:10

Hackoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!