The build takes forty minutes. You walk away, come back, and the laptop has gone to sleep and taken the remote session with it.
On a Mac you would type caffeinate. On a work Windows laptop you cannot install PowerToys, winget is blocked, and you do not have admin rights.
You do have PowerShell.
The problem with the usual fixes
Most advice for this assumes you control the machine: install PowerToys Awake, change the power plan, run a mouse jiggler. On a managed laptop those are blocked, need elevation, or quietly break a policy you agreed to.
I wanted the macOS habit, the same flags, and nothing that IT would reasonably object to.
A caffeinate command for Windows
Keep Windows Awake is a single PowerShell command, caffeinate, that keeps a Windows PC awake with the same flags as macOS. It needs no installer, no PowerToys, no winget and no elevation, and it writes nothing to disk.
It asks Windows not to sleep the same way a video player does, through the SetThreadExecutionState API. It does not press keys or move the mouse.
Quick start
- Open PowerShell. A normal window is enough.
- Open
src/caffeinate.ps1on GitHub, click Copy raw file, paste it into PowerShell and press Enter. That defines the command for this window only. - Run it:
caffeinate -d -t 3600That keeps the system and the display awake for one hour. Press Ctrl+C to stop sooner. When it ends, Windows goes back to its normal sleep settings.
Why pasting works when script files are blocked: the execution policy applies to script files and profiles, not to text typed at the prompt, and Microsoft says it is not a security boundary (docs). It will not work in Constrained Language mode, and the command tells you so in one line.
The flags
caffeinate # keep the system awake until Ctrl+C
caffeinate -d # also keep the display on
caffeinate -t 3600 # stop after 3600 seconds
caffeinate -w 4242 # stop when process 4242 exits
caffeinate -t 3600 -w 4242 # whichever comes first| Flag | macOS caffeinate | This command |
|---|---|---|
-d | Prevent the display from sleeping | Holds the display and the system |
-i | Prevent idle sleep | Accepted. Already the default |
-t <seconds> | Stop after this time | Same |
-w <pid> | Stop when this process exits | Same |
-m, -s, -u | Disk idle, AC sleep, user active | Not supported. Prints a one-line message |
caffeinate -i make | Stay awake while a command runs | Not supported. Use -w <pid> |
Exit codes: 0 success, 2 bad argument, 1 anything else, such as a blocked session.
Which method works on my laptop?
| Your situation | What to use |
|---|---|
| Nothing blocked, or only installers or script files are blocked | caffeinate, pasted into the console |
| PowerShell is in Constrained Language mode (AppLocker or WDAC) | PresentationSettings /start, or the power settings |
| Windows Home edition | The power settings |
| A Group Policy locks the session after inactivity | Nothing here stops it. Ask IT |
Presentation Settings turns on Windows presentation mode, which keeps the PC awake and silences notifications. Run PresentationSettings with no flag first to see what else it changes. Secondary sources say Home editions do not have it.
Power settings change your real, saved values: Settings → System → Power & battery → Screen, sleep & hibernate timeouts, or powercfg /change standby-timeout-ac 120. Write down the old values first (powercfg /query), because this one survives a restart.
What it cannot do
These are limits of the Windows API, documented by Microsoft:
- It cannot stop sleep from the lid, the power button or Start → Sleep.
- It does not stop the screen saver.
- It does not beat a Group Policy inactivity lock. That policy counts user input, and a wake request is not user input.
Tools that fake keypresses or mouse movement are not covered on purpose. Some managed environments treat that as getting around a control. If your employer has rules about what you may run, follow them.
Not sure which restriction you have?
Run these four read-only commands in PowerShell:
$PSVersionTable.PSVersion
$ExecutionContext.SessionState.LanguageMode
Get-ExecutionPolicy -List
(Get-CimInstance Win32_OperatingSystem).Caption| What you see | What to do |
|---|---|
LanguageMode is FullLanguage | Use caffeinate |
LanguageMode is ConstrainedLanguage | Use Presentation Settings or the power settings |
Restricted or AllSigned, or any MachinePolicy/UserPolicy value | Paste the function instead of loading the file |
The edition name contains Home | Use the power settings |
If you see The term 'caffeinate' is not recognized, you opened a new window. A pasted function lasts only for the window you pasted it into, so paste it again or load it from your profile.
Already on a Mac or Linux?
The same job with built-in tools is in docs/macos-linux.md. On a Mac, plain caffeinate -d -t 3600 already does this.
What to do next
- Paste
caffeinate.ps1into a PowerShell window. - Run
caffeinate -d -t 3600before your next long build. - If it says Constrained Language mode, switch to Presentation Settings.







