How-To

PowerShell script to keep your computer awake

By Updated 2026-10-16

Search this and page one is mostly GitHub gists, Reddit threads and a 13-year-old Stack Overflow answer: lots of code, almost no explanation of what it does, which one to pick, or what happens when you stop it. Here are two scripts that work, a plain-English account of how each one behaves, how to run and stop them, and the part the gists never mention: keeping a PC awake is a different thing from keeping a Slack status active.

Quick answer

To keep a Windows PC awake with PowerShell, run a script that calls the SetThreadExecutionState API with the system-required and display-required flags (Script 1 below), or one that presses the F15 key every minute (Script 2). Both run without admin rights on a standard setup, and both undo themselves when you close the script. Neither will reliably keep your Slack status green, because Slack decides if you're active from keyboard and mouse input, not from whether the PC is awake.

Skip the script. Keep the status green.

Stay Green On Slack keeps your Slack presence green from the cloud, on a schedule you set. Nothing runs on your machine. 14-day free trial, no card required.

Start for free →

Script 1: block sleep with SetThreadExecutionState

This is the "correct" way to keep Windows awake from a script, because it's the same call video players and presentation software use. It tells Windows "something is running, don't sleep and don't turn the display off" for as long as the script is alive. It doesn't press any keys or move the cursor, and it needs no admin rights on a standard Windows setup.

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class KeepAwake {
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern uint SetThreadExecutionState(uint esFlags);
}
"@

# 2147483651 = ES_CONTINUOUS (0x80000000) + ES_DISPLAY_REQUIRED (0x2) + ES_SYSTEM_REQUIRED (0x1)
[KeepAwake]::SetThreadExecutionState([uint32]2147483651) | Out-Null
Write-Host "Keeping this PC awake. Press Ctrl+C or close this window to stop."

try {
    while ($true) { Start-Sleep -Seconds 60 }
}
finally {
    # 2147483648 = ES_CONTINUOUS alone: hands control back to your normal power plan
    [KeepAwake]::SetThreadExecutionState([uint32]2147483648) | Out-Null
}

Save it as keepawake.ps1. The flags are the part worth understanding: ES_SYSTEM_REQUIRED stops the PC sleeping, ES_DISPLAY_REQUIRED stops the screen turning off, and ES_CONTINUOUS makes both settings stick until the thread says otherwise. The finally block resets everything when you stop the script, so your normal power plan comes straight back. Nothing is changed permanently.

Script 2: press F15 every minute

The second approach is the one most forum answers give. Instead of telling Windows not to sleep, it creates real keyboard input once a minute. F15 is a good key for this because almost no keyboard has one, so nothing in a document or chat window reacts to it.

$wsh = New-Object -ComObject WScript.Shell
while ($true) {
    $wsh.SendKeys('{F15}')
    Start-Sleep -Seconds 59
}

Because Windows sees a genuine key press, the system idle timer resets, which keeps the screen on and the PC awake, and delays any lock timeout that's based on idle time. It's a little cruder than Script 1, and it's the one endpoint security tools are most likely to notice (more on that below).

How to run and stop a PowerShell keep-awake script

  1. Save the script as keepawake.ps1 in your user folder.
  2. Run it for this session only by opening PowerShell and pasting: powershell -NoProfile -ExecutionPolicy Bypass -File "$HOME\keepawake.ps1". The -ExecutionPolicy Bypass flag applies to that one process, so you don't have to change your system-wide policy. If your employer enforces the policy through Group Policy, this flag won't override it.
  3. Make it double-clickable by putting this single line in a keepawake.bat file next to it:
    powershell -NoProfile -ExecutionPolicy Bypass -File "%USERPROFILE%\keepawake.ps1"
    That's also the answer to the "batch script to keep computer awake" and "cmd script" searches: the .bat file is just a launcher for the PowerShell script above.
  4. Stop it with Ctrl+C or by closing the window. Script 1 hands sleep control back the moment it exits.

If you'd rather change the setting than run a script, the built-in route is powercfg /change standby-timeout-ac 0 (plugged in, never sleep) and powercfg /change monitor-timeout-ac 0 (never turn the screen off). Unlike the scripts, that is a permanent change to your power plan, so you have to put it back yourself.

Which script should you use?

ApproachStops sleepCreates inputEasy to spotUndoes itself
Script 1: SetThreadExecutionStateYesNoLowYes, on exit
Script 2: F15 key pressYesYesHigherYes, on exit
powercfg timeoutsYesNoLowNo, manual
PowerToys AwakeYesNoLowYes

If all you need is a laptop that finishes a download or keeps a presentation on screen, use Script 1, or skip scripting and use PowerToys Awake, which does the same thing with a toggle. Use Script 2 only if you specifically need input to be generated, for example to stop a remote desktop session from timing out on idle.

The catch: an awake PC is not an active Slack status

This is where most people get caught. Script 1 keeps the machine awake, but it never touches the keyboard or mouse, and Slack decides whether you're active from your input, not from whether Windows is asleep. On desktop, Slack marks you away after roughly 10 minutes without input, so a PC that's wide awake can still show a grey dot. We break the whole mechanism down in how to keep Slack active when your computer sleeps or locks and in the full guide to keeping a computer awake.

Script 2 does generate input, so it can delay the away timer, but you're now running a script that fakes activity on a machine your employer may be monitoring. PowerShell logging, endpoint security and software that watches for synthetic input can flag exactly this pattern, and the repetitive once-a-minute rhythm is easy to see in the data. See are mouse jigglers detectable for how that works. Run scripts like this on machines you own, and check your workplace policy before running anything on a managed laptop.

If the real goal is a Slack dot that stays green during your working hours, a script on your PC is the fragile way to do it: it dies when the lid closes, the VPN drops or the laptop reboots. Keeping Slack active from the cloud doesn't depend on your machine at all.

FAQ

How can I keep my computer awake using PowerShell?
Call the Windows SetThreadExecutionState function from a PowerShell script with the system-required and display-required flags, then leave the script running. Windows will not sleep or turn the display off until you stop the script, at which point your normal power settings return. The script above does this in under 15 lines and needs no admin rights on a standard setup.

Can I run a PowerShell keep-awake script without admin rights?
Usually yes. SetThreadExecutionState and the F15 key press method both run as a normal user. Launching the script with -ExecutionPolicy Bypass avoids changing the system policy for that one run. The exception is a managed PC where Group Policy locks the execution policy or blocks scripts entirely, in which case the bypass flag will not help.

How do I keep my Windows PC awake using the command prompt?
Save the PowerShell script as a .ps1 file and launch it from a .bat file or the command prompt with powershell -NoProfile -ExecutionPolicy Bypass -File keepawake.ps1. For a permanent change instead of a running script, powercfg /change standby-timeout-ac 0 stops the PC sleeping while plugged in.

Will a PowerShell script keep my Slack status green?
The SetThreadExecutionState script will not. It keeps the PC awake without generating any keyboard or mouse input, and Slack bases your active status on input, so the dot can still go grey after about 10 minutes. The F15 script generates input, which can delay that timer, but it only works while the script and your machine are running.

Is a keep-awake script detectable?
The SetThreadExecutionState approach looks like any other app asking Windows to stay awake, which is normal behaviour for video and presentation software. A script that sends a key press every minute creates a repeating synthetic-input pattern that endpoint monitoring tools can flag. Check your workplace policy before running either on a company device.

Try it free for 14 days.

No credit card required to start. $3.99/month after the trial. Cancel anytime.

Start for free →
← Back to the blog