Last active
May 13, 2025 21:39
-
-
Save markizano/574e1d3db4b74f908054bf352b6b6ac3 to your computer and use it in GitHub Desktop.
Keeps computer awake all night
This file contains hidden or 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
| Add-Type -TypeDefinition @" | |
| using System; | |
| using System.Runtime.InteropServices; | |
| public static class Kernel32 { | |
| [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] | |
| public static extern uint SetThreadExecutionState(uint esFlags); | |
| public const uint ES_AWAYMODE_REQUIRED = 0x00000040; | |
| public const uint ES_CONTINUOUS = 0x80000000; | |
| public const uint ES_DISPLAY_REQUIRED = 0x00000002; | |
| public const uint ES_SYSTEM_REQUIRED = 0x00000001; | |
| } | |
| "@ | |
| [Kernel32]::SetThreadExecutionState([Kernel32]::ES_CONTINUOUS -bor [Kernel32]::ES_SYSTEM_REQUIRED -bor [Kernel32]::ES_DISPLAY_REQUIRED) | |
| # Optional: Keep the script running indefinitely | |
| Write-Host "Script is running to prevent idle/sleep. Press Ctrl+C to stop." | |
| while ($true) { | |
| # Sleep for a while to avoid excessive CPU usage. | |
| Start-Sleep -Seconds 60 | |
| # Reset the execution state to keep the system awake. | |
| [void][Kernel32]::SetThreadExecutionState([Kernel32]::ES_CONTINUOUS -bor [Kernel32]::ES_SYSTEM_REQUIRED -bor [Kernel32]::ES_DISPLAY_REQUIRED) | |
| date '+%F|%T' | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a 1-liner as well for
SendKeys():