Windows configuration
Set Power & Sleep Settings
Applies power and sleep settings (for example never sleep on AC power).
Replace generic placeholder values (tenant, domain, secrets) for your own environment before running. Read it first and test safely.
# Run as SYSTEM
# Run in 64-bit PowerShell
# Suitable for Intune or any RMM
# Power mode API requires Windows 11
$ErrorActionPreference = 'Stop'
# -----------------------------
# Desired settings
# -----------------------------
# Plugged in
$AcDisplayTimeoutSeconds = 300 # 5 minutes
$AcSleepTimeoutSeconds = 0 # Never
$AcHibernateTimeoutSeconds = 0 # Never
$AcPowerModeGuid = 'ded574b5-45a0-4f42-8737-46345c09c238' # Best Performance
# On battery
$DcDisplayTimeoutSeconds = 180 # 3 minutes
$DcSleepTimeoutSeconds = 900 # 15 minutes
$DcHibernateTimeoutSeconds = 18000 # 300 minutes
$DcPowerModeGuid = '00000000-0000-0000-0000-000000000000' # Balanced
# Base power plan must be Balanced for the power mode UI/slider
$BalancedPlanGuid = '381b4222-f694-41f0-9685-ff5bb260df2e'
function Invoke-PowerCfg {
param(
[Parameter(Mandatory = $true)]
[string[]]$Arguments
)
$proc = Start-Process -FilePath "$env:SystemRoot\System32\powercfg.exe" `
-ArgumentList $Arguments `
-Wait `
-PassThru `
-NoNewWindow
if ($proc.ExitCode -ne 0) {
throw "powercfg failed: powercfg $($Arguments -join ' ') ExitCode=$($proc.ExitCode)"
}
}
# Win32 interop for documented power mode APIs
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class PowerModeNative
{
[DllImport("powrprof.dll", SetLastError = true)]
public static extern UInt32 PowerSetUserConfiguredACPowerMode(ref Guid PowerModeGuid);
[DllImport("powrprof.dll", SetLastError = true)]
public static extern UInt32 PowerSetUserConfiguredDCPowerMode(ref Guid PowerModeGuid);
[DllImport("powrprof.dll", SetLastError = true)]
public static extern UInt32 PowerGetUserConfiguredACPowerMode(out Guid PowerModeGuid);
[DllImport("powrprof.dll", SetLastError = true)]
public static extern UInt32 PowerGetUserConfiguredDCPowerMode(out Guid PowerModeGuid);
}
"@
function Set-PowerMode {
param(
[Parameter(Mandatory = $true)]
[Guid]$AcModeGuid,
[Parameter(Mandatory = $true)]
[Guid]$DcModeGuid
)
$acResult = [PowerModeNative]::PowerSetUserConfiguredACPowerMode([ref]$AcModeGuid)
if ($acResult -ne 0) {
throw "PowerSetUserConfiguredACPowerMode failed with code $acResult"
}
$dcResult = [PowerModeNative]::PowerSetUserConfiguredDCPowerMode([ref]$DcModeGuid)
if ($dcResult -ne 0) {
throw "PowerSetUserConfiguredDCPowerMode failed with code $dcResult"
}
}
function Get-PowerMode {
$acGuid = [Guid]::Empty
$dcGuid = [Guid]::Empty
$acResult = [PowerModeNative]::PowerGetUserConfiguredACPowerMode([ref]$acGuid)
if ($acResult -ne 0) {
throw "PowerGetUserConfiguredACPowerMode failed with code $acResult"
}
$dcResult = [PowerModeNative]::PowerGetUserConfiguredDCPowerMode([ref]$dcGuid)
if ($dcResult -ne 0) {
throw "PowerGetUserConfiguredDCPowerMode failed with code $dcResult"
}
[PSCustomObject]@{
ACPowerMode = $acGuid.Guid
DCPowerMode = $dcGuid.Guid
}
}
try {
$osVersion = [System.Environment]::OSVersion.Version
if ($osVersion.Major -lt 10) {
throw "Unsupported Windows version."
}
Write-Output 'Setting active power plan to Balanced...'
Invoke-PowerCfg -Arguments @('/setactive', $BalancedPlanGuid)
Write-Output 'Setting display timeout values...'
Invoke-PowerCfg -Arguments @('/setacvalueindex', 'SCHEME_CURRENT', 'SUB_VIDEO', 'VIDEOIDLE', $AcDisplayTimeoutSeconds)
Invoke-PowerCfg -Arguments @('/setdcvalueindex', 'SCHEME_CURRENT', 'SUB_VIDEO', 'VIDEOIDLE', $DcDisplayTimeoutSeconds)
Write-Output 'Setting sleep timeout values...'
Invoke-PowerCfg -Arguments @('/setacvalueindex', 'SCHEME_CURRENT', 'SUB_SLEEP', 'STANDBYIDLE', $AcSleepTimeoutSeconds)
Invoke-PowerCfg -Arguments @('/setdcvalueindex', 'SCHEME_CURRENT', 'SUB_SLEEP', 'STANDBYIDLE', $DcSleepTimeoutSeconds)
Write-Output 'Setting hibernate timeout values...'
Invoke-PowerCfg -Arguments @('/setacvalueindex', 'SCHEME_CURRENT', 'SUB_SLEEP', 'HIBERNATEIDLE', $AcHibernateTimeoutSeconds)
Invoke-PowerCfg -Arguments @('/setdcvalueindex', 'SCHEME_CURRENT', 'SUB_SLEEP', 'HIBERNATEIDLE', $DcHibernateTimeoutSeconds)
Write-Output 'Applying power scheme changes...'
Invoke-PowerCfg -Arguments @('/setactive', 'SCHEME_CURRENT')
Write-Output 'Setting power modes...'
$acGuid = [Guid]$AcPowerModeGuid
$dcGuid = [Guid]$DcPowerModeGuid
Set-PowerMode -AcModeGuid $acGuid -DcModeGuid $dcGuid
Start-Sleep -Seconds 2
$modeCheck = Get-PowerMode
Write-Output ''
Write-Output 'Completed successfully.'
Write-Output 'Configured values:'
Write-Output '- Plugged in: Screen off 5 minutes, Sleep Never, Hibernate Never, Power mode Best performance'
Write-Output '- On battery: Screen off 3 minutes, Sleep 15 minutes, Hibernate 300 minutes, Power mode Balanced'
Write-Output ''
Write-Output "Verification:"
Write-Output "- AC power mode GUID: $($modeCheck.ACPowerMode)"
Write-Output "- DC power mode GUID: $($modeCheck.DCPowerMode)"
if ($modeCheck.ACPowerMode -ne $AcPowerModeGuid) {
throw "AC power mode verification failed. Expected $AcPowerModeGuid, got $($modeCheck.ACPowerMode)"
}
if ($modeCheck.DCPowerMode -ne $DcPowerModeGuid) {
throw "DC power mode verification failed. Expected $DcPowerModeGuid, got $($modeCheck.DCPowerMode)"
}
exit 0
}
catch {
Write-Error $_.Exception.Message
exit 1
}