Problem overview
Many Windows devices enter sleep mode by default, even when connected to a power source, causing unexpected downtime and halting critical processes. This script addresses the frustration of losing remote access, interrupting server tasks, or missing automated backups by ensuring devices stay fully operational while on AC power.
<#
This resource is provided as a convenience for Level users. We cannot
guarantee it will work in all environments. Please test before deploying
to your production environment. We welcome contributions to our community
library
Level Library
https://level.io/library/script-windows-disable-sleep-on-ac-power
#>
# Disable sleep on AC power
Write-Host "Disabling sleep on AC power..." -ForegroundColor Green
# Method 1: Using powercfg with correct syntax (recommended)
try {
# Set the AC power setting for sleep timeout to never (0 minutes)
$result = powercfg /change standby-timeout-ac 0
if ($LASTEXITCODE -eq 0) {
Write-Host "Sleep on AC power has been disabled successfully." -ForegroundColor Green
} else {
throw "PowerCfg command failed with exit code: $LASTEXITCODE"
}
} catch {
Write-Host "Method 1 failed. Trying alternative method..." -ForegroundColor Yellow
# Method 2: Using GUID-based approach
try {
# Get the current active power scheme GUID
$currentScheme = (powercfg /getactivescheme).Split()[3]
# Set sleep timeout on AC to 0 (never) using GUIDs
# SUB_SLEEP GUID: 238c9fa8-0aad-41ed-83f4-97be242c8f20
# STANDBYAC GUID: 29f6c1db-86da-48c5-9fdb-f2b67b1f44da
powercfg /setacvalueindex $currentScheme 238c9fa8-0aad-41ed-83f4-97be242c8f20 29f6c1db-86da-48c5-9fdb-f2b67b1f44da 0
# Apply the changes
powercfg /setactive $currentScheme
if ($LASTEXITCODE -eq 0) {
Write-Host "Sleep on AC power has been disabled successfully using alternative method." -ForegroundColor Green
} else {
throw "Alternative method also failed"
}
} catch {
Write-Host "Failed to disable sleep on AC power. Please check your permissions or try running as Administrator." -ForegroundColor Red
Write-Host "Error details: $($_.Exception.Message)" -ForegroundColor Red
}
}
# Verify the change
Write-Host "`nVerifying changes..." -ForegroundColor Cyan
try {
$verification = powercfg /query SCHEME_CURRENT SUB_SLEEP STANDBYAC 2>&1
# Check if Group Policy is overriding settings
if ($verification -match "Group policy override settings exist") {
Write-Host "WARNING: Group Policy is controlling power settings on this system." -ForegroundColor Yellow
Write-Host "The script may have applied changes, but Group Policy will override them." -ForegroundColor Yellow
Write-Host "Contact your IT administrator to modify Group Policy power settings." -ForegroundColor Yellow
# Try to show what the effective setting would be without GP
Write-Host "`nAttempting to show configured setting (may differ from effective setting due to GP):" -ForegroundColor Cyan
powercfg /query SCHEME_CURRENT SUB_SLEEP STANDBYAC 2>$null | Select-String -Pattern "Current Setting"
}
elseif ($verification -match "Invalid Parameters") {
Write-Host "Unable to verify due to parameter issues. Trying alternative verification..." -ForegroundColor Yellow
# Alternative verification using export
$tempFile = [System.IO.Path]::GetTempFileName()
powercfg /export $tempFile 2>$null
if (Test-Path $tempFile) {
Write-Host "Settings export successful - changes likely applied." -ForegroundColor Green
Remove-Item $tempFile -Force
}
}
elseif ($verification -match "Current Setting.*0x00000000") {
Write-Host "Verification successful: Sleep on AC power is disabled (0 minutes)" -ForegroundColor Green
}
else {
Write-Host "Current AC sleep timeout setting:" -ForegroundColor Yellow
$verification | Select-String -Pattern "Current Setting" | ForEach-Object { Write-Host $_.Line -ForegroundColor White }
}
} catch {
Write-Host "Could not verify the setting change: $($_.Exception.Message)" -ForegroundColor Yellow
}
# Additional Group Policy check
Write-Host "`nChecking for Group Policy restrictions..." -ForegroundColor Cyan
$gpResult = gpresult /r /scope:computer 2>&1 | Select-String -Pattern "Power Management"
if ($gpResult) {
Write-Host "Group Policy power management settings detected:" -ForegroundColor Yellow
$gpResult | ForEach-Object { Write-Host " $($_.Line)" -ForegroundColor White }
} This script leverages the built-in Windows powercfg utility to set the sleep timeout value on AC power to zero, effectively disabling sleep mode. Once it makes the change, it validates the current setting to confirm sleep has indeed been disabled, offering peace of mind that the adjustment is successful.
Use cases
- Keeping remote systems awake to maintain uninterrupted access
- Ensuring virtual machines or servers do not enter sleep mode during critical operations
- Preventing kiosk or public-facing machines from powering down unexpectedly
- Maintaining a consistent environment for reliable backup or patch routines
Recommendations
- Test the script on a non-production device or virtual machine to confirm it applies the settings correctly
- Use Level Level to run this on new devices or as part of setup to ensure you have consistent management access
- For recurring adjustments, build an Automation in Level that runs on a schedule to ensure all newly onboarded devices remain awake