A PowerShell module that prevents Windows from sleeping -- a drop-in equivalent of macOS caffeinate.
Uses the Win32 SetThreadExecutionState API (the same mechanism used by video players and Teams calls) to hold sleep-prevention assertions.
Install-Module -Name PSCaffeinate -Scope CurrentUser# Import (auto-imports if installed via Install-Module)
Import-Module PSCaffeinatecaffeinatecaffeinate -d -t 3600caffeinate -w (Get-Process robocopy).IdArguments after the command name are forwarded automatically -- no parens needed:
caffeinate python train.py --epochs 100
caffeinate robocopy C:\src D:\backup /MIR /MT:8Note: Do not wrap the command in parentheses.
caffeinate (python train.py)would executepythonimmediately in a subexpression, defeating the purpose.
caffeinate -disu
caffeinate -Flags di -t 7200| Parameter | Alias | Description |
|---|---|---|
-Flags |
POSIX-style bundled flags: disu = -d -i -s -u |
|
-PreventDisplaySleep |
-d |
Prevent the display from sleeping |
-PreventIdleSleep |
-i |
Prevent system idle sleep (default if no flag given) |
-PreventSystemSleep |
-s |
Prevent system sleep (same as -i on Windows) |
-UserActive |
-u |
Assert user is active (resets idle/screensaver timer) |
-Timeout |
-t |
Release after N seconds |
-WaitPid |
-w |
Release when a specific PID exits |
-Command |
Run a command and release when it finishes | |
-ArgumentList |
Arguments passed to -Command |
Invoke-Caffeinate supports -WhatIf and -Confirm:
caffeinate -d -t 60 -WhatIf
# What if: Performing the operation "Hold" on target "sleep assertions [display, system-idle]".- Windows (uses
kernel32.dll) - PowerShell 5.1 or later (Desktop or Core edition)
See CONTRIBUTING.md for guidelines.