Skip to content

Instantly share code, notes, and snippets.

Last active September 21, 2020 23:26
What would you like to do?
Keep Windows Awake Temporarily (Prevent Sleep, Display Off or Away Mode) #cli #powershell #windows
#!/usr/bin/env powershell
# This script can keep the computer awake while executing another executable, or
# if no executable was passed in, then it stays awake until this script stops.
# There are 3 different ways of staying awake:
# Away Mode - Enable away mode (
# Display Mode - Keep the display on and don't go to sleep or hibernation
# System Mode - Don't go to sleep or hibernation
# The default mode is the System Mode.
# Away mode is only available when away mode is enabled in the advanced power options.
# These commands are advisory, the option to allow programs to request to disable
# sleep or display off is in advanced power options.
# The above options will need to be first enabled in the registry before you can
# see them in the advanced power options.
# An alternative to this script is using presentation mode, but this is more flexible.
param (
[string]$Executable = $null,
[ValidateSet('Away', 'Display', 'System')]$Option = 'System'
[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
$ste = Add-Type -memberDefinition $Code -name System -namespace Win32 -passThru
# Requests that the other EXECUTION_STATE flags set remain in effect until
# SetThreadExecutionState is called again with the ES_CONTINUOUS flag set and
# one of the other EXECUTION_STATE flags cleared.
$ES_CONTINUOUS = [uint32]"0x80000000"
$ES_AWAYMODE_REQUIRED = [uint32]"0x00000040"
$ES_DISPLAY_REQUIRED = [uint32]"0x00000002"
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001"
Switch ($Option) {
"Away" {$Setting = $ES_AWAYMODE_REQUIRED}
"Display" {$Setting = $ES_DISPLAY_REQUIRED}
"System" {$Setting = $ES_SYSTEM_REQUIRED}
try {
Write-Host "Staying Awake with ``${Option}`` Option"
$ste::SetThreadExecutionState($ES_CONTINUOUS -bor $Setting)
if ($Executable) {
Write-Host "Executing Executable"
& "$Executable"
} else {
Read-Host "Enter or Exit to Stop Staying Awake"
} finally {
Write-Host "Stopping Staying Awake"
Copy link

curtisalexander commented Jun 1, 2019

@CMCDragonkai - what is the license for this script? I would like to fork and make some minor changes to then host in a separate repo and want to make sure I am crediting you as well as respecting your intent with the license. Thanks.

Copy link

Consider it APL-2.0

Copy link

Thank you!

Copy link

ghost commented Jun 30, 2019

"$ES_DISPLAY_REQUIRED" is not working. Monitor still goes to sleep. I think it must be called periodically.

Copy link

ghost commented Jun 30, 2019

Seems that when done like this then it will keep the monitor awake


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment