ysskrishna logoysskrishna
Cover image for caffeinate for Windows: Keep a Locked-Down Laptop Awake Without Installing Anything

caffeinate for Windows: Keep a Locked-Down Laptop Awake Without Installing Anything

ysskrishna profile photoysskrishna
4 min read
DevOps & Infrastructure
PowerShell

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

  1. Open PowerShell. A normal window is enough.
  2. Open src/caffeinate.ps1 on GitHub, click Copy raw file, paste it into PowerShell and press Enter. That defines the command for this window only.
  3. Run it:
caffeinate -d -t 3600

That 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
FlagmacOS caffeinateThis command
-dPrevent the display from sleepingHolds the display and the system
-iPrevent idle sleepAccepted. Already the default
-t <seconds>Stop after this timeSame
-w <pid>Stop when this process exitsSame
-m, -s, -uDisk idle, AC sleep, user activeNot supported. Prints a one-line message
caffeinate -i makeStay awake while a command runsNot 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 situationWhat to use
Nothing blocked, or only installers or script files are blockedcaffeinate, pasted into the console
PowerShell is in Constrained Language mode (AppLocker or WDAC)PresentationSettings /start, or the power settings
Windows Home editionThe power settings
A Group Policy locks the session after inactivityNothing 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 seeWhat to do
LanguageMode is FullLanguageUse caffeinate
LanguageMode is ConstrainedLanguageUse Presentation Settings or the power settings
Restricted or AllSigned, or any MachinePolicy/UserPolicy valuePaste the function instead of loading the file
The edition name contains HomeUse 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

  1. Paste caffeinate.ps1 into a PowerShell window.
  2. Run caffeinate -d -t 3600 before your next long build.
  3. If it says Constrained Language mode, switch to Presentation Settings.

Further reading

Continue exploring

BlogCover image for OSC 7501 in Python: Tell the Terminal When Your CLI Is Working, Blocked or Done

OSC 7501 in Python: Tell the Terminal When Your CLI Is Working, Blocked or Done

Python
Python CLI
+1
3 min read
ProductPyPIDocker Detector

Docker Detector

One call tells your Python app whether it is running inside Docker.
DevOps & Infrastructure
PyPI Packages
Python
uv
ProductPyPIOSC 7501

OSC 7501

Tell your terminal when your CLI is working, blocked, finished, or failed.
DevOps & Infrastructure
PyPI Packages
Python
Python CLI
ProductPyPIContainer Detect

Container Detect

One Python call to learn if your process is inside Docker, Podman, Kubernetes, or another Linux container.
DevOps & Infrastructure
PyPI Packages
Python
uv
ProductGitHub MarketplaceRelease from Changelog or Tag

Release from Changelog or Tag

The changelog section you already wrote becomes the GitHub Release.
DevOps & Infrastructure
GitHub Marketplace Actions
GitHub Actions
Bash
ProductNextjs Multi-Stage Docker

Nextjs Multi-Stage Docker

See the real size gap between one-stage and multi-stage Next.js Docker builds.
DevOps & Infrastructure
Starter Kits & Boilerplates
Docker
Next.js