Keep PC Awake Script (Anti-AFK)
Create a Windows script that keeps your PC awake and the screen on for a set time, with clear stop steps.
Asks Windows not to sleep or turn off the screen while the script runs. Nothing is typed or clicked.
No script needed? Microsoft PowerToys has Awake, which does the same from the system tray. Or set sleep to Never in Settings > System > Power.
Check your workplace rules first: some employers don't allow tools that keep a PC awake or simulate activity, and security software may block scripts.
KeepAwake.ps1
# Keep PC Awake - made with ToolKit360
# Stops Windows from sleeping and turning off the screen while this window is open, for 2 hours.
# It asks Windows directly (SetThreadExecutionState); it does not press keys or move the mouse.
#
# How to run: right-click this file > Run with PowerShell (Windows 11: Show more options first).
# How to stop: close this window or press Ctrl+C. Normal sleep settings return straight away.
$minutes = 120 # how long to keep awake; 0 = until you close this window
Add-Type -Namespace ToolKit360 -Name Power -MemberDefinition '[DllImport("kernel32.dll")] public static extern uint SetThreadExecutionState(uint esFlags);'
# ES_CONTINUOUS (0x80000000) + ES_SYSTEM_REQUIRED (0x1) + ES_DISPLAY_REQUIRED (0x2)
$keepAwake = [uint32]2147483651
$normal = [uint32]2147483648
$Host.UI.RawUI.WindowTitle = 'Keep PC Awake - close this window to stop'
if ($minutes -gt 0) { $end = (Get-Date).AddMinutes($minutes) } else { $end = [datetime]::MaxValue }
try {
if ($minutes -gt 0) {
Write-Host ("Keeping this PC awake until {0:HH:mm}. Close this window to stop sooner." -f $end)
} else {
Write-Host 'Keeping this PC awake. Close this window or press Ctrl+C to stop.'
}
while ((Get-Date) -lt $end) {
[void][ToolKit360.Power]::SetThreadExecutionState($keepAwake)
Start-Sleep -Seconds 30
}
} finally {
[void][ToolKit360.Power]::SetThreadExecutionState($normal)
Write-Host 'Stopped. Windows is back to its normal sleep settings.'
}
- How to run
- Right-click KeepAwake.ps1 and choose Run with PowerShell (on Windows 11, Show more options first). Double-clicking only opens it in Notepad. If Windows says running scripts is disabled, open PowerShell in that folder and run:
powershell -ExecutionPolicy Bypass -File .\KeepAwake.ps1 - How to stop
- Close the PowerShell window or press Ctrl+C in it. Windows goes back to its normal sleep settings straight away.
Keep PC Awake Script (Anti-AFK) FAQ
How do I stop my Windows PC from going to sleep?
Download the Stop sleep script, right-click it and choose Run with PowerShell. While its window is open Windows won't sleep; close the window to go back to normal. For a permanent change, set sleep to Never in Settings > System > Power.
Is there a way without a script?
Yes. Microsoft PowerToys includes Awake, which keeps the PC awake from an icon in the system tray, and Windows Settings lets you set screen and sleep timeouts to Never.
Why does my .ps1 file open in Notepad?
Windows opens PowerShell scripts in an editor on double-click to stop them running by accident. Right-click the file and choose Run with PowerShell instead.
What is the difference between the two methods?
Stop sleep tells Windows not to sleep or dim the screen, the same way video players do, and types nothing. The key-press method sends F15 so programs that watch for keyboard input see activity; use it only when a program needs that.
Is this the same as a mouse jiggler?
The key-press script works like the Caffeine app, pressing an unused key instead of moving the mouse. The recommended Stop sleep script is different: it only keeps Windows awake and does not fake any input.
Does the script send any data?
No. The scripts only call Windows on your own PC; they make no network connections and change no settings permanently. This page builds them in your browser.