简体   繁体   中英

Batch file that closes all programs except certain ones

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.

if you just want to close the programs that you open. which is shown in task manager as App 在此处输入图像描述

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

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 (not tested)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM