脚本库\3_系统硬件配置清理.bat 编码: UTF-8
151 5,217 5.58 KB
返回
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

cls
echo ==========================================
echo    系统硬件配置清理工具 v2.0.3   2026-8-6
echo ==========================================
echo.

REM ========== 检测PE环境 ==========
set "is_pe=0"
set "pe_drive="

if /i "%SystemDrive%"=="X:" set "is_pe=1"
if exist "%SystemDrive%\Windows\System32\winpeshl.exe" set "is_pe=1"

if "!is_pe!"=="1" (
    set "pe_drive=%SystemDrive:~0,1%"
    echo [PE环境] 检测到PE系统,PE系统盘符:!pe_drive!:(已排除)
    echo.
    echo 正在扫描本地Windows系统...
    echo.
    set "sysdrive="
    for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
        if exist "%%d:\" (
            if /i not "%%d"=="!pe_drive!" (
                set "found_sys=0"
                if exist "%%d:\Windows\System32\explorer.exe" set "found_sys=1"
                if exist "%%d:\Windows\System32\kernel32.dll" set "found_sys=1"
                if exist "%%d:\Windows\System32\ntoskrnl.exe" set "found_sys=1"
                if exist "%%d:\Windows\System32\config\SYSTEM" set "found_sys=1"
                if exist "%%d:\Windows\WinSxS" set "found_sys=1"
                if exist "%%d:\Windows\Boot" set "found_sys=1"
                if "!found_sys!"=="1" (
                    echo   %%d: 盘  [Windows系统]
                    if "!sysdrive!"=="" set "sysdrive=%%d"
                )
            )
        )
    )
    if "!sysdrive!"=="" (
        echo [错误] 未检测到任何Windows系统!请确认硬盘连接或系统是否存在。
        pause
        exit /b
    )
    echo.
    echo 默认目标系统盘:!sysdrive!:
    echo.
) else (
    set "sysdrive=%SystemDrive:~0,1%"
    echo 系统所在盘符:!sysdrive!:
    echo.
    echo 检测本机所有驱动器:
    for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
        if exist "%%d:\" (
            echo   %%d: 盘
        )
    )
    echo.
)

REM ========== 选择盘符 ==========
:input_drive
set "TargetDrive=!sysdrive!"
set /p TargetDrive=请输入目标系统盘符(默认 !sysdrive!:,直接回车确认): 

if not defined TargetDrive set "TargetDrive=!sysdrive!"
set "TargetDrive=%TargetDrive::=%"

echo.
echo 正在验证目标系统...
set "MissingPaths="
if not exist "%TargetDrive%:\Windows\" set "MissingPaths=!MissingPaths! - %TargetDrive%:\Windows\n"
if not exist "%TargetDrive%:\Windows\System32\" set "MissingPaths=!MissingPaths! - %TargetDrive%:\Windows\System32\n"
if not exist "%TargetDrive%:\Windows\System32\config\SYSTEM" set "MissingPaths=!MissingPaths! - %TargetDrive%:\Windows\System32\config\SYSTEM\n"
if not exist "%TargetDrive%:\Windows\System32\config\SOFTWARE" set "MissingPaths=!MissingPaths! - %TargetDrive%:\Windows\System32\config\SOFTWARE\n"

if defined MissingPaths (
    echo 错误:目标系统盘不完整或非系统盘
    echo 缺失的路径:
    echo !MissingPaths!
    pause
    goto input_drive
)

echo 验证通过!
echo.
echo ==========================================
echo            正在执行深度清理
echo ==========================================
echo.
echo 目标盘: %TargetDrive%:
echo.

echo [1/4] 加载系统注册表...
reg load HKLM\TempHive "%TargetDrive%:\Windows\System32\config\SYSTEM" 2>nul
if %errorlevel% neq 0 (
    echo 错误:无法加载注册表!
    pause
    goto input_drive
)

echo [2/4] 清除硬件配置...
reg delete "HKEY_LOCAL_MACHINE\TempHive\HardwareConfig" /f 2>nul
reg delete "HKEY_LOCAL_MACHINE\TempHive\Select" /f 2>nul
reg add "HKEY_LOCAL_MACHINE\TempHive\Select" /v "Current" /t REG_DWORD /d 1 /f 2>nul
reg add "HKEY_LOCAL_MACHINE\TempHive\Select" /v "Default" /t REG_DWORD /d 1 /f 2>nul
echo       完成

echo [3/4] 清除存储驱动历史...
reg delete "HKEY_LOCAL_MACHINE\TempHive\System\ControlSet001\Services\Disk\Enum" /f 2>nul
reg delete "HKEY_LOCAL_MACHINE\TempHive\System\ControlSet001\Services\atapi" /f 2>nul
reg delete "HKEY_LOCAL_MACHINE\TempHive\System\ControlSet001\Services\iaStorV" /f 2>nul
reg delete "HKEY_LOCAL_MACHINE\TempHive\System\ControlSet001\Services\stornvme" /f 2>nul
reg delete "HKEY_LOCAL_MACHINE\TempHive\System\ControlSet001\Control\Class\{4d36e967-e325-11ce-bfc1-08002be10318}" /f 2>nul
echo       完成

echo [4/4] 清除设备枚举历史...
reg delete "HKEY_LOCAL_MACHINE\TempHive\System\ControlSet001\Enum\USB" /f 2>nul
reg delete "HKEY_LOCAL_MACHINE\TempHive\System\ControlSet001\Enum\PCI" /f 2>nul
reg delete "HKEY_LOCAL_MACHINE\TempHive\System\ControlSet001\Enum\ACPI" /f 2>nul
reg unload HKLM\TempHive 2>nul
echo       完成

echo.
echo [5/6] 加载软件注册表...
reg load HKLM\TempHive2 "%TargetDrive%:\Windows\System32\config\SOFTWARE" 2>nul
if %errorlevel% equ 0 (
    reg delete "HKEY_LOCAL_MACHINE\TempHive2\Microsoft\Windows\CurrentVersion\Setup\PnpLockdownFiles" /f 2>nul
    reg delete "HKEY_LOCAL_MACHINE\TempHive2\Microsoft\Windows\CurrentVersion\Setup\DiskPnpList" /f 2>nul
    reg delete "HKEY_LOCAL_MACHINE\TempHive2\Microsoft\Windows\CurrentVersion\RunOnce" /f 2>nul
    reg unload HKLM\TempHive2 2>nul
    echo       完成
)

echo [6/6] 清理事件日志...
if exist "%TargetDrive%:\Windows\System32\winevt\Logs\" (
    del /q "%TargetDrive%:\Windows\System32\winevt\Logs\*.evtx" 2>nul
)
echo       完成

echo.
echo ==========================================
echo          清理完成!
echo ==========================================
echo.
echo 提示:重启后系统将重新检测所有硬件
echo.
timeout /t 3 /nobreak >nul
exit /b 0