Skip to content

Instantly share code, notes, and snippets.

@markizano
Last active May 13, 2025 21:39
Show Gist options
  • Select an option

  • Save markizano/574e1d3db4b74f908054bf352b6b6ac3 to your computer and use it in GitHub Desktop.

Select an option

Save markizano/574e1d3db4b74f908054bf352b6b6ac3 to your computer and use it in GitHub Desktop.
Keeps computer awake all night
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'
}
@markizano
Copy link
Author

As a 1-liner as well for SendKeys():

$wsh = New-Object -ComObject WScript.Shell; while (1) { $wsh.SendKeys('+{F15}'); Start-Sleep -seconds 59; date }

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