Last active
November 28, 2024 05:21
-
-
Save CMCDragonkai/bf8e8b7553c48e4f65124bc6f41769eb to your computer and use it in GitHub Desktop.
Keep Windows Awake Temporarily (Prevent Sleep, Display Off or Away Mode) #cli #powershell #windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 (https://blogs.msdn.microsoft.com/david_fleischman/2005/10/21/what-does-away-mode-do-anyway/) | |
# 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' | |
) | |
$Code=@' | |
[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" | |
$ste::SetThreadExecutionState($ES_CONTINUOUS) | |
} |
Consider it APL-2.0
Thank you!
"$ES_DISPLAY_REQUIRED" is not working. Monitor still goes to sleep. I think it must be called periodically.
Seems that when done like this then it will keep the monitor awake
$ste::SetThreadExecutionState($ES_SYSTEM_REQUIRED -bor $ES_DISPLAY_REQUIRED -bor $ES_CONTINUOUS )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.