使用CMD关闭所有程序除外

时间:2017-09-06 21:27:32

标签: batch-file cmd

我有这个代码: 这是批处理文件代码

@echo off
title Kill all running apps - Bharat Balegere - AgniPulse.com
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" 
echo.
)
)
)
)
)
pause

但它将关闭所有程序

我尝试编辑它并添加类似这样的内容

if not "%%i"=="notepad.exe" (
但它不工作,当我点击批处理文件什么都没有 所以我不知道如何编辑这个 并把一些除了它! 提前谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试将此批处理文件与白名单一起用于终止进程

当然,您可以根据需要修改白名单变量:

@echo off
Title Kill all running apps - Bharat Balegere - AgniPulse.com
set "whitelist=avast mbam dllhost conhost" 
Set "whitelist=%whitelist% taskeng skype"
Set "whitelist=%whitelist% firefox explorer cmd"
Set "whitelist=%whitelist% chrome tasklist taskmgr notepad"

@For /f "skip=3 tokens=1" %%i in (
'TASKLIST /FI "USERNAME eq %userdomain%\%username%" /FI "STATUS eq running" ^| findstr /VI "%whitelist%"'
) Do ( 
    echo Taskkill /IM "%%i" /F
)
pause & exit

如果您希望此列表被杀死,您当然可以删除echo命令!

编辑: 这是另一个使用WMIC

的白名单批次
@echo off
Title Process killer with a white list
Mode con cols=50 lines=40 & color 9B
setlocal
Set "White_Process_List=%~dpn0_White_Process_List.txt"
set "Ignoring_Process_List=%~dpn0_Ignoring_Process_List.txt"
set "Terimnated_Process_List=%~dpn0_Terimnated_List.txt"
If exist "%Ignoring_Process_List%" Del "%Ignoring_Process_List%"
If exist "%Terimnated_Process_List%" Del "%Terimnated_Process_List%"

:Whitelist
set "whitelist=Microsoft Mozilla Google Avast Panda ESET Kaspersky Avira AVG Bitdefender Malwarebytes Norton McAfee GAS IBM Skype"
If not exist "%White_Process_List%" echo %whitelist% > "%White_Process_List%" 
Set /p whitelist=<"%White_Process_List%"

:Analyze
for /f "tokens=2 delims=," %%I in (
    'wmic process get executablepath^,status /format:csv ^| find "\"'
) do (
    set "proc=%%~I"
    setlocal enabledelayedexpansion
    wmic datafile where "name='!proc:\=\\!'" get manufacturer 2>nul | findstr /i "%whitelist%" >nul && (
        ( echo Ignoring : %%~nxI & echo "%%~I" 
         echo --------------------------------------- )>>!Ignoring_Process_List! 2>&1
    ) || (
        ( echo Terminating: %%~nxI )>con
        ( echo Terminating : %%~nxI & echo "%%~I" 
        Taskkill /im "%%~nxI" /f /t 
        echo --------------------------------------- )>>!Terimnated_Process_List! 2>&1
    )
    endlocal 
)
Timeout /T 2 /nobreak>nul