@echo off title 系统优化脚本 - 管理员权限运行 color 0A REM 检查管...

Created on: December 13, 2025

Answered using GPT-5.2 Thinking by Chat01

Question

@echo off
title 系统优化脚本 - 管理员权限运行
color 0A

REM 检查管理员权限
net session >nul 2>&1
if %errorLevel% neq 0 (
echo 请以管理员身份运行此脚本!
echo 右键点击脚本,选择"以管理员身份运行"
pause
exit /b 1
)

echo ========================================
echo 系统优化配置脚本
echo ========================================
echo.
echo 正在执行以下任务:
echo 1. 关闭鼠标"增强指针精度"
echo 2. 设置虚拟内存为物理内存的1.5倍
echo 3. 配置高性能电源计划
echo 4. 验证修改结果
echo.
echo 注意:某些更改可能需要重启才能生效。
echo ========================================
pause
cls

:MAIN_MENU
echo ============= 主菜单 =============
echo 1. 执行所有任务
echo 2. 关闭鼠标增强指针精度
echo 3. 设置虚拟内存
echo 4. 配置电源计划
echo 5. 验证当前设置
echo 6. 恢复默认设置
echo 7. 退出
echo =================================
set /p choice="请选择操作 (1-7): "

if "%choice%"=="1" goto ALL_TASKS
if "%choice%"=="2" goto MOUSE_SETTINGS
if "%choice%"=="3" goto VIRTUAL_MEMORY
if "%choice%"=="4" goto POWER_PLAN
if "%choice%"=="5" goto VERIFY_SETTINGS
if "%choice%"=="6" goto RESTORE_DEFAULTS
if "%choice%"=="7" exit /b
echo 无效选择,请重新输入
goto MAIN_MENU

:ALL_TASKS
call :MOUSE_SETTINGS
call :VIRTUAL_MEMORY
call :POWER_PLAN
call :VERIFY_SETTINGS
goto MAIN_MENU

:MOUSE_SETTINGS
cls
echo [任务1] 关闭鼠标"增强指针精度"
echo.
echo 正在修改鼠标设置...
echo.

REM 方法1:通过注册表直接修改
reg add "HKCU\Control Panel\Mouse" /v "MouseSpeed" /t REG_SZ /d "0" /f >nul
reg add "HKCU\Control Panel\Mouse" /v "MouseThreshold1" /t REG_SZ /d "0" /f >nul
reg add "HKCU\Control Panel\Mouse" /v "MouseThreshold2" /t REG_SZ /d "0" /f >nul

REM 方法2:使用PowerShell命令(更可靠)
powershell -Command "Set-ItemProperty -Path 'HKCU:\Control Panel\Mouse' -Name 'MouseSpeed' -Value '0'"
powershell -Command "Set-ItemProperty -Path 'HKCU:\Control Panel\Mouse' -Name 'MouseThreshold1' -Value '0'"
powershell -Command "Set-ItemProperty -Path 'HKCU:\Control Panel\Mouse' -Name 'MouseThreshold2' -Value '0'"

REM 方法3:使用控制面板命令(针对Win10/Win11)
powershell -Command "Get-ItemProperty -Path 'HKCU:\Control Panel\Mouse' | Out-Null"

echo 鼠标设置已完成!
echo 注意:可能需要注销或重启才能完全生效。
echo.
echo 您也可以手动验证:
echo 1. 打开"设置" -> "蓝牙和其他设备" -> "鼠标"
echo 2. 点击"其他鼠标设置"
echo 3. 在"指针选项"中取消勾选"提高指针精确度"
echo.
pause
cls
goto :eof

:VIRTUAL_MEMORY
cls
echo [任务2] 设置虚拟内存
echo.
echo 正在检测物理内存...

REM 获取物理内存大小(MB) - 使用systeminfo命令更可靠
for /f "tokens=2" %%A in ('systeminfo ^| find "物理内存总量"') do (
set "memStr=%%A"
)
if "%memStr%"=="" (
for /f "tokens=2" %%A in ('systeminfo ^| find "Total Physical Memory"') do (
set "memStr=%%A"
)
)

REM 清理字符串并转换为MB
set "memStr=%memStr:,=%"
set "memStr=%memStr:MB=%"
set "memStr=%memStr:GB=%"

REM 判断单位并转换
echo %memStr% | find "GB" >nul
if %errorLevel% equ 0 (
set /a totalMB=%memStr:GB=%*1024
) else (
set /a totalMB=%memStr%
)

set /a virtualMB=%totalMB%*3/2

echo.
echo 物理内存: %totalMB% MB (~%totalMB:0,-3% GB)
echo 推荐虚拟内存: %virtualMB% MB (
%virtualMB:~0,-3% GB)
echo.

echo 正在设置虚拟内存...
echo 注意:这需要一些时间,请稍候...
echo.

REM 创建PowerShell脚本设置虚拟内存
echo System=GetWmiObjectClassWin32ComputerSystemEnableAllPrivileges>echoSystem = Get-WmiObject -Class Win32_ComputerSystem -EnableAllPrivileges > %temp%\SetPagefile.ps1 echo System.AutomaticManagedPagefile = False>>echoFalse >> %temp%\SetPagefile.ps1 echo System.Put() >> %temp%\SetPagefile.ps1
echo. >> %temp%\SetPagefile.ps1
echo CurrentPageFile=GetWmiObjectQuery"SelectFromWin32PageFileSettingWhereNamelikeechoif(CurrentPageFile = Get-WmiObject -Query "Select * From Win32_PageFileSetting Where Name like '%pagefile.sys'" >> %temp%\SetPagefile.ps1 echo if (CurrentPageFile) { CurrentPageFile.Delete() } >> %temp%\SetPagefile.ps1 echo. >> %temp%\SetPagefile.ps1 echo PageFile = ([wmiclass]"win32_pagefilesetting").CreateInstance() >> %temp%\SetPagefile.ps1
echo PageFile.Name="PageFile.Name = "env:SystemDrive\pagefile.sys" >> %temp%\SetPagefile.ps1
echo PageFile.InitialSize=echoPageFile.InitialSize = %virtualMB% >> %temp%\SetPagefile.ps1 echo PageFile.MaximumSize = %virtualMB% >> %temp%\SetPagefile.ps1
echo $PageFile.Put() >> %temp%\SetPagefile.ps1
echo "虚拟内存已设置为 %virtualMB% MB" >> %temp%\SetPagefile.ps1

REM 以管理员权限运行PowerShell
powershell -ExecutionPolicy Bypass -File "%temp%\SetPagefile.ps1"

if %errorLevel% equ 0 (
echo 虚拟内存设置成功!
echo 已设置为: %virtualMB% MB (~%virtualMB:~0,-3% GB)
) else (
echo.
echo 虚拟内存设置可能需要手动操作。
echo 请按以下步骤手动设置:
echo 1. 按 Win+R,输入 sysdm.cpl
echo 2. 转到"高级"选项卡
echo 3. 点击"性能"部分的"设置"
echo 4. 转到"高级"选项卡
echo 5. 点击"虚拟内存"部分的"更改"
echo 6. 取消勾选"自动管理所有驱动器的分页文件大小"
echo 7. 选择系统驱动器,选择"自定义大小"
echo 8. 输入初始大小和最大大小: %virtualMB% MB
echo 9. 点击"设置",然后"确定"
)

del %temp%\SetPagefile.ps1 >nul 2>&1

echo.
echo 注意:需要重启才能生效。
echo.
pause
cls
goto :eof

:POWER_PLAN
cls
echo [任务3] 配置电源计划
echo.
echo 正在配置高性能电源计划...
echo.

REM 检查是否已有高性能计划
for /f "tokens=*" %%A in ('powercfg /list ^| findstr /i "高性能"') do (
set "powerPlanLine=%%A"
)

if defined powerPlanLine (
echo 发现高性能电源计划...
for /f "tokens=2 delims=:(" %%A in ("%powerPlanLine%") do (
set "highPerfGuid=%%A"
set "highPerfGuid=!highPerfGuid: =!"
)
) else (
echo 创建高性能游戏电源计划...

text
REM 获取平衡计划的GUID for /f "tokens=*" %%A in ('powercfg /list ^| findstr /i "平衡"') do ( set "balancePlanLine=%%A" ) for /f "tokens=2 delims=:(" %%A in ("%balancePlanLine%") do ( set "balanceGuid=%%A" set "balanceGuid=!balanceGuid: =!" ) REM 复制平衡计划并重命名为高性能游戏模式 for /f "tokens=1" %%A in ('powercfg -duplicate %balanceGuid%') do ( set "newGuid=%%A" ) powercfg -changename %newGuid% "高性能游戏模式" "为游戏优化的电源计划,降低延迟提升性能" set "highPerfGuid=%newGuid%"

)

REM 激活高性能计划
powercfg -setactive %highPerfGuid%

echo 正在优化电源计划设置...

REM 关键设置:处理器性能
powercfg -setacvalueindex %highPerfGuid% 54533251-82be-4824-96c1-47b60b740d00 75b0ae3f-bce0-45a7-8c89-c9611c25e100 100
powercfg -setdcvalueindex %highPerfGuid% 54533251-82be-4824-96c1-47b60b740d00 75b0ae3f-bce0-45a7-8c89-c9611c25e100 100

REM 关闭USB选择性暂停
powercfg -setacvalueindex %highPerfGuid% 2a737441-1930-4402-8d77-b2bebba308a3 48e6b7a6-50f5-4782-a5d4-53bb8f07e226 0
powercfg -setdcvalueindex %highPerfGuid% 2a737441-1930-4402-8d77-b2bebba308a3 48e6b7a6-50f5-4782-a5d4-53bb8f07e226 0

REM PCI Express最大性能
powercfg -setacvalueindex %highPerfGuid% 501a4d13-42af-4429-9fd1-a8218c268e20 ee12f906-d277-404b-b6da-e5fa1a576df5 0
powercfg -setdcvalueindex %highPerfGuid% 501a4d13-42af-4429-9fd1-a8218c268e20 ee12f906-d277-404b-b6da-e5fa1a576df5 0

REM 硬盘不休眠
powercfg -setacvalueindex %highPerfGuid% 0012ee47-9041-4b5d-9b77-535fba8b1442 6738e2c4-e8a5-4a42-b16a-e040e769756e 0
powercfg -setdcvalueindex %highPerfGuid% 0012ee47-9041-4b5d-9b77-535fba8b1442 6738e2c4-e8a5-4a42-b16a-e040e769756e 0

REM 应用所有更改
powercfg -setactive %highPerfGuid%

echo.
echo 电源计划配置完成!
echo 当前活动计划: 高性能游戏模式
echo 注意:某些设置可能需要重启才能完全生效。
echo.
pause
cls
goto :eof

:VERIFY_SETTINGS
cls
echo [任务4] 验证当前设置
echo ========================================
echo.

echo 1. 鼠标设置验证:
echo ========================================
echo 检查鼠标增强指针精度设置...
reg query "HKCU\Control Panel\Mouse" /v "MouseSpeed" 2>nul | findstr /i "0x0|0"
if %errorLevel% equ 0 (
echo √ 注册表键值 MouseSpeed = 0(增强指针精度已禁用)
) else (
reg query "HKCU\Control Panel\Mouse" /v "MouseSpeed" 2>nul
if %errorLevel% equ 1 (
echo ! 注册表键值不存在,可能使用默认设置
) else (
echo × 鼠标增强指针精度可能未正确禁用
)
)

echo.
echo 提示:您也可以手动验证:
echo 1. 打开"设置" -> "蓝牙和其他设备" -> "鼠标"
echo 2. 点击"其他鼠标设置"
echo 3. 在"指针选项"中查看"提高指针精确度"是否未勾选
echo.

echo 2. 虚拟内存验证:
echo ========================================
echo 当前分页文件设置:
wmic pagefile list brief /format:list 2>nul | findstr "CurrentSize"
if %errorLevel% neq 0 (
echo 正在使用PowerShell查询虚拟内存...
powershell -Command "Get-WmiObject -Class Win32_PageFileUsage | Select-Object Name, @{Name='Size(MB)';Expression={_.AllocatedBaseSize}}, @{Name='Used(MB)';Expression={_.CurrentUsage}} | Format-List"
)

echo.
echo 系统推荐的虚拟内存设置:物理内存的1.5倍
systeminfo | findstr /i "物理内存总量"
systeminfo | findstr /i "Total Physical Memory"

echo.

echo 3. 电源计划验证:
echo ========================================
echo 当前活动电源计划:
powercfg /getactivescheme

echo.
echo 所有可用电源计划:
powercfg /list

echo.
echo 高性能计划详细信息:
for /f "tokens=*" %%A in ('powercfg /list ^| findstr /i "高性能"') do (
echo %%A
)

echo.

echo 4. 系统信息:
echo ========================================
echo 操作系统版本:
ver
systeminfo | findstr /B /C:"OS 名称" /C:"OS Version"

echo.
echo 内存信息:
systeminfo | findstr /B /C:"物理内存总量" /C:"可用物理内存" /C:"虚拟内存"
systeminfo | findstr /B /C:"Total Physical Memory" /C:"Available Physical Memory" /C:"Virtual Memory"

echo.
echo 5. 建议的手动验证步骤:
echo ========================================
echo 虚拟内存:按 Win+R,输入 sysdm.cpl
echo 转到"高级"->"性能设置"->"高级"->"虚拟内存"
echo.
echo 电源计划:控制面板->硬件和声音->电源选项
echo.
echo 鼠标设置:控制面板->硬件和声音->鼠标->指针选项
echo (取消勾选"提高指针精确度")
echo.

echo 按任意键返回主菜单...
pause >nul
cls
goto MAIN_MENU

:RESTORE_DEFAULTS
cls
echo [恢复默认设置]
echo.
echo 警告:这将恢复系统默认设置!
echo.
set /p confirm="确定要恢复默认设置吗?(y/n): "
if /i not "%confirm%"=="y" goto MAIN_MENU

echo 正在恢复默认设置...
echo.

echo 1. 恢复鼠标默认设置...
reg delete "HKCU\Control Panel\Mouse" /v "MouseSpeed" /f >nul 2>&1
reg delete "HKCU\Control Panel\Mouse" /v "MouseThreshold1" /f >nul 2>&1
reg delete "HKCU\Control Panel\Mouse" /v "MouseThreshold2" /f >nul 2>&1

echo 2. 恢复虚拟内存为系统管理...
powershell -Command "System=GetWmiObjectClassWin32ComputerSystemEnableAllPrivileges;System = Get-WmiObject -Class Win32_ComputerSystem -EnableAllPrivileges; System.AutomaticManagedPagefile = True;True; System.Put()"

echo 3. 恢复平衡电源计划...
for /f "tokens=*" %%A in ('powercfg /list ^| findstr /i "平衡"') do (
for /f "tokens=2 delims=:(" %%B in ("%%A") do (
set "balanceGuid=%%B"
set "balanceGuid=!balanceGuid: =!"
)
)
if defined balanceGuid (
powercfg -setactive %balanceGuid%
echo 已激活平衡电源计划
)

echo.
echo 默认设置已恢复!
echo 注意:部分更改需要重启才能生效。
echo.
pause
cls
goto MAIN_MENU

找出代码中的问题:
[任务4] 验证当前设置

  1. 鼠标设置验证:
    ========================================
    检查鼠标增强指针精度设置...

HKEY_CURRENT_USER\Control Panel\Mouse
MouseSpeed REG_SZ 0

! 注册表键值不存在,可能使用默认设置

提示:您也可以手动验证:
2. 点击"其他鼠标设置"
3. 在"指针选项"中查看"提高指针精确度"是否未勾选

  1. 虚拟内存验证:
    ========================================
    当前分页文件设置:
    正在使用PowerShell查询虚拟内存...

Name : C:\pagefile.sys
Size(MB) : 6144
Used(MB) : 0

系统推荐的虚拟内存设置:物理内存的1.5倍
物理内存总量: 48,988 MB

  1. 电源计划验证:
    ========================================
    当前活动电源计划:
    电源方案 GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (高性能)
    所有可用电源计划:

现有电源使用方案 (* Active)

电源方案 GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (平衡)
电源方案 GUID: 6cb74058-1b9f-4d29-97f9-c747317834f6 (卓越性能)
电源方案 GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (高性能) *
电源方案 GUID: a1841308-3541-4fab-bc81-f71556f20b4a (节能)
电源方案 GUID: acb63093-0032-41aa-9cb8-1eb2f98b28e4 (Ultimate Performance - Low Latency)

高性能计划详细信息:
电源方案 GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (高性能) *

  1. 系统信息:
    ========================================
    操作系统版本:

Microsoft Windows [版本 10.0.22631.6199]
OS 名称: Microsoft Windows 11 专业版

内存信息:

  1. 建议的手动验证步骤:
    ========================================
    虚拟内存:按 Win+R,输入 sysdm.cpl

       (取消勾选"提高指针精确度")
    

按任意键返回主菜单...

Thought

Analyzing batch script error logic

I need to analyze a batch script where the output shows a conflict: it prints that the registry value exists, but then says the key doesn't exist. The issue seems to be with errorLevel logic. In batch, %errorLevel% expands at parse time within parentheses, causing unexpected outcomes. I need to confirm this behavior.

Identifying the error in errorlevel expansion

I think the issue is that the errorlevel from findstr persists after the first check, causing the if %errorLevel% equ 1 inside the else to use the wrong value. Since %errorLevel% expands at parse time, it's using the old value when the second reg query runs. That appears to be the source of the incorrect output.

Fixing batch script bugs and improving logic

To fix the batch script, I’ll need to use delayed expansion (setlocal EnableDelayedExpansion and !errorlevel!) for correct errorlevel checks within blocks. Also, the findstr pattern is flawed since \| doesn’t act as an OR. I’ll try adding findstr /R /C:"MouseSpeed *REG_SZ *0$" or use a for loop for comparison.

Fixing issues with GB conversion and expansion

The script has a couple of bugs: the GB conversion check fails because it's removed earlier, causing find "GB" to always fail. Also, removing "GB" results in missing units, and using totalMB:0,-3 isn’t an accurate conversion to GB. Additionally, delayed expansion isn’t properly enabled for the power plan part, leading to issues with variable expansion (e.g., !highPerfGuid!). Without delayed expansion, it could cause errors in GUID parsing.

Fixing conflicting messages in Verify section

The problem arises because findstr doesn't support \| for OR, and without /R or proper tokens, the search string doesn't match, causing an errorlevel mismatch. As a result, the script enters the else block and checks %errorLevel%, which still holds the previous value (1). This leads to contradictory output. To fix, enabling delayed expansion and correcting the findstr pattern will solve the issue.

Thought for 1m 10s

Answer

你这个输出里最明显的“自相矛盾”点是:

已经查到 MouseSpeed = 0,但脚本又打印了“注册表键值不存在”。

这不是系统问题,是 脚本判断逻辑写错了(主要在 [任务4] 验证当前设置 的鼠标验证段)。


1) findstr "0x0\|0" 的匹配写法是错的(导致必进 else)

你写的是:

bat
reg query "HKCU\Control Panel\Mouse" /v "MouseSpeed" 2>nul | findstr /i "0x0\|0" if %errorLevel% equ 0 ( echo √ ... ) else ( ... )

问题点:

  • reg query 输出的是类似:MouseSpeed REG_SZ 0
  • 你用 findstr /i "0x0\|0" 期望匹配 00x0
  • Windows 的 findstr 不支持 | 作为“或”(它不是 grep),\| 也不会变成 OR
  • 所以它并不会匹配到单独的 0findstr 往往返回 1 → 直接进入 else

这就是为什么你明明看到输出里有 0,脚本还走了失败分支。

✅建议:不要用这种“伪正则 OR”,改成更精确的匹配,例如只匹配行尾是 0:

bat
reg query "HKCU\Control Panel\Mouse" /v "MouseSpeed" 2>nul | findstr /R /C:"MouseSpeed *REG_SZ *0$" >nul

2) 在括号块里用 %errorLevel% 会被“提前展开”(导致错误提示乱跳)

你在 else 块里又写了:

bat
reg query "HKCU\Control Panel\Mouse" /v "MouseSpeed" 2>nul if %errorLevel% equ 1 ( echo ! 注册表键值不存在... ) else ( echo × ... )

在批处理里,(...) 是整块先解析再执行。块内的 %errorLevel% 可能会拿到“进入块之前”的值,而不是上一条 reg query 执行后的值,于是就会出现:

  • reg query 明明成功打印了键值
  • 但紧接着 if %errorLevel% equ 1 还按旧值判断 → 打印“键值不存在”

✅修法有两种(任选一种):

修法 A(推荐):用 && / ||,避免 errorlevel 变量坑

bat
reg query "HKCU\Control Panel\Mouse" /v "MouseSpeed" 2>nul ^ | findstr /R /C:"MouseSpeed *REG_SZ *0$" >nul ^ && echo √ MouseSpeed = 0(增强指针精度已禁用) ^ || echo × MouseSpeed 不是 0 或查询失败

修法 B:开启延迟展开,用 !errorlevel!

在脚本开头加:

bat
setlocal EnableDelayedExpansion

然后块里改为:

bat
reg query ... 2>nul if !errorlevel! equ 1 ( ... )

3) 你当前“鼠标验证”只查了 MouseSpeed,漏了两个关键键

你关闭增强指针精度时写了三项:

  • MouseSpeed
  • MouseThreshold1
  • MouseThreshold2

但验证只查了 MouseSpeed。即便 MouseSpeed=0,如果阈值没变,也可能不一致。

✅建议验证三项都查:

bat
for %%K in (MouseSpeed MouseThreshold1 MouseThreshold2) do ( reg query "HKCU\Control Panel\Mouse" /v "%%K" 2>nul | findstr /R /C:"REG_SZ *0$" >nul ^ && echo √ %%K = 0 ^ || echo × %%K 未正确设置 )

顺带再指出两个“会埋雷”的问题(不在任务4但你脚本里确实存在)

A) 你脚本大量用了 !var!,但没有 EnableDelayedExpansion

比如电源计划段落里有:

bat
set "highPerfGuid=!highPerfGuid: =!"

如果没开延迟展开,这句不会按你想的工作,GUID 去空格会失败,后面 powercfg 可能用到脏 GUID。

✅同样:脚本一开始就应该:

bat
setlocal EnableExtensions EnableDelayedExpansion

B) 虚拟内存物理内存解析逻辑有明显 bug(GB 判断永远不可能成立)

你先把 GB 删除了:

bat
set "memStr=%memStr:GB=%"

后面又判断:

bat
echo %memStr% | find "GB"

那肯定永远找不到 GB。


最小改动版:把你“鼠标验证”那段改成这样就不会再矛盾

直接替换你 VERIFY_SETTINGS 里鼠标验证部分:

bat
echo 检查鼠标增强指针精度设置... reg query "HKCU\Control Panel\Mouse" /v "MouseSpeed" 2>nul ^ | findstr /R /C:"MouseSpeed *REG_SZ *0$" >nul ^ && ( echo √ MouseSpeed = 0(增强指针精度已禁用) ) || ( echo × MouseSpeed 未正确禁用或查询失败 reg query "HKCU\Control Panel\Mouse" /v "MouseSpeed" 2>nul )

这样:

  • 不依赖 %errorlevel% 在块内的展开时机
  • 不用 \| 这种 findstr 不支持的“或”
  • 输出不会再出现“查到了 0 但又说不存在”的情况

如果你愿意,我也可以把整个脚本做一次“只改必要点”的修复版:

  1. 全局加 delayed expansion;2) 修虚拟内存解析;3) 修电源计划 GUID 提取/重命名;4) verify 全面且稳定。你直接把你想兼容的系统范围(Win10/11?中英文?)说一下就行。

Share this Q&A