
本脚本可以直接copy到power shell运行。也可以保存为psl后执行
保存建议:
用记事本打开
复制上面的修正代码
文件 → 另存为
文件名:Disable-Sleep.ps1
保存类型:所有文件
编码:ANSI
运行:
powershell -ExecutionPolicy Bypass -File ".\Disable-Sleep.ps1"运行脚本效果:


脚本:
# 工控机电源策略强化工具
# 科控物联 QQ: 2492123056
# 生产线专用 - 确保7x24小时不间断运行
# 检查管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "错误:请使用管理员权限运行此脚本!" -ForegroundColor Red
Write-Host "右键点击 PowerShell 图标,选择以管理员身份运行" -ForegroundColor Yellow
pause
exit
}
# 初始化错误计数器
$errorCount = 0
$successCount = 0
Write-Host ""
Write-Host "==================================================" -ForegroundColor Green
Write-Host " 工控机电源策略强化部署工具" -ForegroundColor Red
Write-Host " 科控物联 QQ: 2492123056" -ForegroundColor Green
Write-Host "==================================================" -ForegroundColor Green
Write-Host "正在禁用所有休眠功能..." -ForegroundColor Yellow
# 1. 禁用休眠功能
Write-Host ""
Write-Host "[1/7] 正在禁用休眠功能..." -ForegroundColor Cyan
try {
$result = powercfg -h off 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " 完成 休眠功能已禁用" -ForegroundColor Green
$successCount++
} else {
Write-Host " 失败 无法禁用休眠功能" -ForegroundColor Red
Write-Host " 错误信息: $result" -ForegroundColor Red
$errorCount++
}
} catch {
Write-Host " 失败 执行禁用休眠时发生异常" -ForegroundColor Red
$errorCount++
}
# 2. 禁用屏幕保护程序
Write-Host ""
Write-Host "[2/7] 正在禁用屏幕保护程序..." -ForegroundColor Cyan
try {
$result1 = reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveActive /t REG_SZ /d 0 /f 2>&1
$result2 = reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveTimeOut /t REG_SZ /d 0 /f 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " 完成 屏幕保护程序已禁用" -ForegroundColor Green
$successCount++
} else {
Write-Host " 失败 无法禁用屏幕保护程序" -ForegroundColor Red
Write-Host " 错误信息: $result1 $result2" -ForegroundColor Red
$errorCount++
}
} catch {
Write-Host " 失败 执行禁用屏幕保护时发生异常" -ForegroundColor Red
$errorCount++
}
# 3. 设置显示器和硬盘永不休眠
Write-Host ""
Write-Host "[3/7] 正在设置显示器和硬盘超时..." -ForegroundColor Cyan
try {
$commands = @(
"powercfg -change -standby-timeout-ac 0",
"powercfg -change -standby-timeout-dc 0",
"powercfg -change -monitor-timeout-ac 0",
"powercfg -change -monitor-timeout-dc 0",
"powercfg -change -disk-timeout-ac 0",
"powercfg -change -disk-timeout-dc 0"
)
$allSuccess = $true
foreach ($cmd in $commands) {
$result = Invoke-Expression $cmd 2>&1
if ($LASTEXITCODE -ne 0) {
$allSuccess = $false
Write-Host " 警告 命令执行失败: $cmd" -ForegroundColor Yellow
}
}
if ($allSuccess) {
Write-Host " 完成 显示器和硬盘永不休眠" -ForegroundColor Green
$successCount++
} else {
Write-Host " 部分失败 某些超时设置可能未生效" -ForegroundColor Yellow
$errorCount++
}
} catch {
Write-Host " 失败 设置超时时发生异常" -ForegroundColor Red
$errorCount++
}
# 4. 禁用混合睡眠
Write-Host ""
Write-Host "[4/7] 正在禁用混合睡眠..." -ForegroundColor Cyan
try {
$result1 = powercfg -setacvalueindex SCHEME_CURRENT 238c9fa8-0aad-41ed-83f4-97be242c8f20 94ac6d29-73ce-41a6-809f-6363ba21b47e 0 2>&1
$result2 = powercfg -setdcvalueindex SCHEME_CURRENT 238c9fa8-0aad-41ed-83f4-97be242c8f20 94ac6d29-73ce-41a6-809f-6363ba21b47e 0 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " 完成 混合睡眠已禁用" -ForegroundColor Green
$successCount++
} else {
Write-Host " 失败 无法禁用混合睡眠" -ForegroundColor Red
Write-Host " 错误信息: $result1 $result2" -ForegroundColor Red
$errorCount++
}
} catch {
Write-Host " 失败 禁用混合睡眠时发生异常" -ForegroundColor Red
$errorCount++
}
# 5. 禁用无人参与睡眠
Write-Host ""
Write-Host "[5/7] 正在禁用无人参与睡眠..." -ForegroundColor Cyan
try {
$result1 = powercfg -setacvalueindex SCHEME_CURRENT 238c9fa8-0aad-41ed-83f4-97be242c8f20 29f6c1db-86da-48c5-9fdb-f2b67b1f44da 0 2>&1
$result2 = powercfg -setdcvalueindex SCHEME_CURRENT 238c9fa8-0aad-41ed-83f4-97be242c8f20 29f6c1db-86da-48c5-9fdb-f2b67b1f44da 0 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " 完成 无人参与睡眠已禁用" -ForegroundColor Green
$successCount++
} else {
Write-Host " 失败 无法禁用无人参与睡眠" -ForegroundColor Red
Write-Host " 错误信息: $result1 $result2" -ForegroundColor Red
$errorCount++
}
} catch {
Write-Host " 失败 禁用无人参与睡眠时发生异常" -ForegroundColor Red
$errorCount++
}
# 6. 禁用快速启动
Write-Host ""
Write-Host "[6/7] 正在禁用快速启动..." -ForegroundColor Cyan
try {
$result = reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Power" /v HiberbootEnabled /t REG_DWORD /d 0 /f 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " 完成 快速启动已禁用" -ForegroundColor Green
$successCount++
} else {
Write-Host " 失败 无法禁用快速启动" -ForegroundColor Red
Write-Host " 错误信息: $result" -ForegroundColor Red
$errorCount++
}
} catch {
Write-Host " 失败 禁用快速启动时发生异常" -ForegroundColor Red
$errorCount++
}
# 7. 应用高性能电源方案
Write-Host ""
Write-Host "[7/7] 正在设置高性能电源方案..." -ForegroundColor Cyan
try {
$result = powercfg -setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " 完成 高性能方案已激活" -ForegroundColor Green
$successCount++
} else {
Write-Host " 失败 无法激活高性能方案" -ForegroundColor Red
Write-Host " 错误信息: $result" -ForegroundColor Red
$errorCount++
}
} catch {
Write-Host " 失败 激活高性能方案时发生异常" -ForegroundColor Red
$errorCount++
}
# 验证结果
Write-Host ""
Write-Host "==================================================" -ForegroundColor Green
Write-Host "验证配置状态..." -ForegroundColor Green
Write-Host "==================================================" -ForegroundColor Green
try {
$sleepState = powercfg -a 2>&1
if ($LASTEXITCODE -eq 0) {
if ($sleepState -like "*Standby (S3)*") {
Write-Host "警告:系统仍支持睡眠状态,请手动检查!" -ForegroundColor Yellow
Write-Host "请检查控制面板中的电源选项" -ForegroundColor Yellow
} else {
Write-Host "成功:所有睡眠功能已禁用" -ForegroundColor Green
}
} else {
Write-Host "警告:无法验证睡眠状态" -ForegroundColor Yellow
}
} catch {
Write-Host "验证睡眠状态时发生错误" -ForegroundColor Red
}
# 显示最终结果统计
Write-Host ""
Write-Host "==================================================" -ForegroundColor Green
Write-Host "部署完成统计:" -ForegroundColor Cyan
Write-Host "成功项目: $successCount/7" -ForegroundColor Green
Write-Host "失败项目: $errorCount/7" -ForegroundColor $(if ($errorCount -gt 0) { "Red" } else { "Green" })
Write-Host "==================================================" -ForegroundColor Green
if ($errorCount -gt 0) {
Write-Host "注意:有 $errorCount 个项目设置失败!" -ForegroundColor Red
Write-Host "建议检查系统权限或手动设置电源选项" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "重要建议:" -ForegroundColor Cyan
Write-Host "1. 立即重启计算机" -ForegroundColor White
Write-Host "2. 检查控制面板中的电源选项" -ForegroundColor White
Write-Host "3. 确认所有睡眠选项设置为从不" -ForegroundColor White
Write-Host "4. 确保没有自动维护任务" -ForegroundColor White
Write-Host ""
Write-Host "==================================================" -ForegroundColor Green
Write-Host "此配置确保24小时不间断运行" -ForegroundColor Red
Write-Host "==================================================" -ForegroundColor Green
# 重启提示
$restart = Read-Host "是否立即重启计算机?(y/n)"
if ($restart -eq 'y' -or $restart -eq 'Y') {
Write-Host "正在重启计算机..." -ForegroundColor Yellow
Restart-Computer -Force
} else {
Write-Host ""
Write-Host "请手动重启以完成设置!" -ForegroundColor Yellow
Write-Host "按任意键退出..." -ForegroundColor Yellow
pause
}如果需要日志,可以参考下面的脚本:
# 工控机电源策略强化工具 - 完整优化版
# 科控物联 QQ: 2492123056
# 生产线专用 - 确保7x24小时不间断运行
# 初始化日志系统
$logDir = "C:\PowerConfigLogs"
$logFile = "$logDir\PowerConfig_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
# 创建日志目录
if (!(Test-Path $logDir)) {
New-Item -ItemType Directory -Path $logDir -Force | Out-Null
}
# 日志记录函数
function Write-Log {
param(
[string]$Message,
[string]$Level = "INFO"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$timestamp [$Level] $Message"
# 输出到屏幕
switch ($Level) {
"ERROR" { Write-Host $logEntry -ForegroundColor Red }
"WARN" { Write-Host $logEntry -ForegroundColor Yellow }
"INFO" { Write-Host $logEntry -ForegroundColor White }
"SUCCESS" { Write-Host $logEntry -ForegroundColor Green }
default { Write-Host $logEntry }
}
# 写入日志文件
Add-Content -Path $logFile -Value $logEntry -Encoding UTF8
}
# 检查管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Log "错误:请使用管理员权限运行此脚本!" "ERROR"
Write-Log "右键点击 PowerShell 图标,选择以管理员身份运行" "WARN"
pause
exit
}
# 初始化错误计数器
$errorCount = 0
$successCount = 0
Write-Log "==================================================" "INFO"
Write-Log " 工控机电源策略强化部署工具" "INFO"
Write-Log " 科控物联 QQ: 2492123056" "INFO"
Write-Log " 日志文件: $logFile" "INFO"
Write-Log "==================================================" "INFO"
Write-Log "正在禁用所有休眠功能..." "INFO"
# 记录系统信息
Write-Log "系统信息收集开始..." "INFO"
Write-Log "计算机名: $env:COMPUTERNAME" "INFO"
Write-Log "用户名: $env:USERNAME" "INFO"
Write-Log "操作系统: $(Get-WmiObject -Class Win32_OperatingSystem).Caption" "INFO"
Write-Log "系统架构: $(Get-WmiObject -Class Win32_OperatingSystem).OSArchitecture" "INFO"
# 1. 禁用休眠功能
Write-Log "[1/7] 正在禁用休眠功能..." "INFO"
try {
$result = powercfg -h off 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "完成 休眠功能已禁用" "SUCCESS"
$successCount++
} else {
Write-Log "失败 无法禁用休眠功能" "ERROR"
Write-Log "错误信息: $result" "ERROR"
$errorCount++
}
} catch {
Write-Log "失败 执行禁用休眠时发生异常: $($_.Exception.Message)" "ERROR"
$errorCount++
}
# 2. 禁用屏幕保护程序
Write-Log "[2/7] 正在禁用屏幕保护程序..." "INFO"
try {
$result1 = reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveActive /t REG_SZ /d 0 /f 2>&1
$result2 = reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveTimeOut /t REG_SZ /d 0 /f 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "完成 屏幕保护程序已禁用" "SUCCESS"
$successCount++
} else {
Write-Log "失败 无法禁用屏幕保护程序" "ERROR"
Write-Log "错误信息: $result1 $result2" "ERROR"
$errorCount++
}
} catch {
Write-Log "失败 执行禁用屏幕保护时发生异常: $($_.Exception.Message)" "ERROR"
$errorCount++
}
# 3. 设置显示器和硬盘永不休眠
Write-Log "[3/7] 正在设置显示器和硬盘超时..." "INFO"
try {
$commands = @(
"powercfg -change -standby-timeout-ac 0",
"powercfg -change -standby-timeout-dc 0",
"powercfg -change -monitor-timeout-ac 0",
"powercfg -change -monitor-timeout-dc 0",
"powercfg -change -disk-timeout-ac 0",
"powercfg -change -disk-timeout-dc 0"
)
$allSuccess = $true
foreach ($cmd in $commands) {
$result = Invoke-Expression $cmd 2>&1
if ($LASTEXITCODE -ne 0) {
$allSuccess = $false
Write-Log "警告 命令执行失败: $cmd" "WARN"
Write-Log "错误信息: $result" "WARN"
}
}
if ($allSuccess) {
Write-Log "完成 显示器和硬盘永不休眠" "SUCCESS"
$successCount++
} else {
Write-Log "部分失败 某些超时设置可能未生效" "WARN"
$errorCount++
}
} catch {
Write-Log "失败 设置超时时发生异常: $($_.Exception.Message)" "ERROR"
$errorCount++
}
# 4. 禁用混合睡眠
Write-Log "[4/7] 正在禁用混合睡眠..." "INFO"
try {
$result1 = powercfg -setacvalueindex SCHEME_CURRENT 238c9fa8-0aad-41ed-83f4-97be242c8f20 94ac6d29-73ce-41a6-809f-6363ba21b47e 0 2>&1
$result2 = powercfg -setdcvalueindex SCHEME_CURRENT 238c9fa8-0aad-41ed-83f4-97be242c8f20 94ac6d29-73ce-41a6-809f-6363ba21b47e 0 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "完成 混合睡眠已禁用" "SUCCESS"
$successCount++
} else {
Write-Log "失败 无法禁用混合睡眠" "ERROR"
Write-Log "错误信息: $result1 $result2" "ERROR"
$errorCount++
}
} catch {
Write-Log "失败 禁用混合睡眠时发生异常: $($_.Exception.Message)" "ERROR"
$errorCount++
}
# 5. 禁用无人参与睡眠
Write-Log "[5/7] 正在禁用无人参与睡眠..." "INFO"
try {
$result1 = powercfg -setacvalueindex SCHEME_CURRENT 238c9fa8-0aad-41ed-83f4-97be242c8f20 29f6c1db-86da-48c5-9fdb-f2b67b1f44da 0 2>&1
$result2 = powercfg -setdcvalueindex SCHEME_CURRENT 238c9fa8-0aad-41ed-83f4-97be242c8f20 29f6c1db-86da-48c5-9fdb-f2b67b1f44da 0 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "完成 无人参与睡眠已禁用" "SUCCESS"
$successCount++
} else {
Write-Log "失败 无法禁用无人参与睡眠" "ERROR"
Write-Log "错误信息: $result1 $result2" "ERROR"
$errorCount++
}
} catch {
Write-Log "失败 禁用无人参与睡眠时发生异常: $($_.Exception.Message)" "ERROR"
$errorCount++
}
# 6. 禁用快速启动
Write-Log "[6/7] 正在禁用快速启动..." "INFO"
try {
$result = reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Power" /v HiberbootEnabled /t REG_DWORD /d 0 /f 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "完成 快速启动已禁用" "SUCCESS"
$successCount++
} else {
Write-Log "失败 无法禁用快速启动" "ERROR"
Write-Log "错误信息: $result" "ERROR"
$errorCount++
}
} catch {
Write-Log "失败 禁用快速启动时发生异常: $($_.Exception.Message)" "ERROR"
$errorCount++
}
# 7. 应用高性能电源方案
Write-Log "[7/7] 正在设置高性能电源方案..." "INFO"
try {
$result = powercfg -setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "完成 高性能方案已激活" "SUCCESS"
$successCount++
} else {
Write-Log "失败 无法激活高性能方案" "ERROR"
Write-Log "错误信息: $result" "ERROR"
$errorCount++
}
} catch {
Write-Log "失败 激活高性能方案时发生异常: $($_.Exception.Message)" "ERROR"
$errorCount++
}
# 验证结果
Write-Log "==================================================" "INFO"
Write-Log "验证配置状态..." "INFO"
Write-Log "==================================================" "INFO"
try {
$sleepState = powercfg -a 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "电源状态检测结果:" "INFO"
Write-Log "------------------------------------------" "INFO"
# 格式化输出电源状态信息
$sleepStateLines = $sleepState -split "`r`n"
foreach ($line in $sleepStateLines) {
if ($line.Trim() -ne "") {
Write-Log " $($line.Trim())" "INFO"
}
}
Write-Log "------------------------------------------" "INFO"
# 修正检测逻辑
if ($sleepState -like "*待机 (S3)*" -and $sleepState -notlike "*不支持此待机状态*") {
Write-Log "信息:硬件支持待机(S3)状态(正常)" "INFO"
Write-Log "但电源策略已设置为永不休眠" "INFO"
} else {
Write-Log "成功:待机(S3)状态已禁用或不受支持" "SUCCESS"
}
if ($sleepState -like "*休眠*" -and $sleepState -notlike "*尚未启用休眠*" -and $sleepState -notlike "*休眠不可用*") {
Write-Log "警告:系统仍支持休眠状态!" "WARN"
} else {
Write-Log "成功:休眠状态已禁用" "SUCCESS"
}
if ($sleepState -like "*混合睡眠*" -and $sleepState -notlike "*休眠不可用*") {
Write-Log "警告:系统仍支持混合睡眠!" "WARN"
} else {
Write-Log "成功:混合睡眠已禁用" "SUCCESS"
}
# 检查快速启动注册表项来确认状态
$fastBoot = Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power" -Name "HiberbootEnabled" -ErrorAction SilentlyContinue
if ($fastBoot -and $fastBoot.HiberbootEnabled -eq 0) {
Write-Log "成功:快速启动已禁用" "SUCCESS"
} else {
Write-Log "警告:快速启动可能仍启用!" "WARN"
}
} else {
Write-Log "警告:无法验证睡眠状态" "WARN"
Write-Log "错误信息: $sleepState" "WARN"
}
} catch {
Write-Log "验证睡眠状态时发生错误: $($_.Exception.Message)" "ERROR"
}
# 显示最终结果统计
Write-Log "==================================================" "INFO"
Write-Log "部署完成统计:" "INFO"
Write-Log "成功项目: $successCount/7" $(if ($successCount -eq 7) { "SUCCESS" } else { "INFO" })
Write-Log "失败项目: $errorCount/7" $(if ($errorCount -gt 0) { "ERROR" } else { "INFO" })
Write-Log "日志文件: $logFile" "INFO"
Write-Log "==================================================" "INFO"
# 记录最终状态到日志
Write-Log "最终状态: 成功 $successCount 项, 失败 $errorCount 项" "INFO"
Write-Log "脚本执行完成时间: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" "INFO"
if ($errorCount -gt 0) {
Write-Log "注意:有 $errorCount 个项目设置失败!" "ERROR"
Write-Log "建议检查系统权限或手动设置电源选项" "WARN"
}
Write-Log ""
Write-Log "重要建议:" "INFO"
Write-Log "1. 立即重启计算机" "INFO"
Write-Log "2. 检查控制面板中的电源选项" "INFO"
Write-Log "3. 确认所有睡眠选项设置为从不" "INFO"
Write-Log "4. 确保没有自动维护任务" "INFO"
Write-Log "==================================================" "INFO"
Write-Log "此配置确保24小时不间断运行" "INFO"
Write-Log "==================================================" "INFO"
# 重启提示
$restart = Read-Host "是否立即重启计算机?(y/n)"
if ($restart -eq 'y' -or $restart -eq 'Y') {
Write-Log "用户选择立即重启计算机" "INFO"
Write-Log "正在重启计算机..." "INFO"
Restart-Computer -Force
} else {
Write-Log "用户选择不立即重启" "INFO"
Write-Log "请手动重启以完成设置!" "WARN"
Write-Log "按任意键退出..." "INFO"
pause
}日志

重启电脑后查看状态:
# 检查电源设置
powercfg -q
powercfg -a
# 检查快速启动状态
reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Power" /v HiberbootEnabled