Skip to content

Instantly share code, notes, and snippets.

@mikeplate
Created June 4, 2026 07:42
Show Gist options
  • Select an option

  • Save mikeplate/fc4bb589dddcac9a3ce09b163ef141c8 to your computer and use it in GitHub Desktop.

Select an option

Save mikeplate/fc4bb589dddcac9a3ce09b163ef141c8 to your computer and use it in GitHub Desktop.
Awake - PowerShell script to keep computer running
Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class Power {
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
[DllImport("kernel32.dll", SetLastError=true)]
public static extern uint SetThreadExecutionState(uint f);
}
"@
if ([Power]::SetThreadExecutionState(
[Power]::ES_CONTINUOUS -bor [Power]::ES_SYSTEM_REQUIRED
) -eq 0) {
throw "Failed: $([Runtime.InteropServices.Marshal]::GetLastWin32Error())"
}
Read-Host "Press Enter to allow sleep again"
[Power]::SetThreadExecutionState([Power]::ES_CONTINUOUS) | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment