Skip to content

Instantly share code, notes, and snippets.

@crpietschmann
Last active March 18, 2023 19:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crpietschmann/eed4467834998ff161c6bea58c675342 to your computer and use it in GitHub Desktop.
Save crpietschmann/eed4467834998ff161c6bea58c675342 to your computer and use it in GitHub Desktop.
PowerShell Script to Keep PC Awake
## KeepAwake.ps1
## Use SendKeys to toggle the Scroll Lock key
##
## This will keep the PC awake and prevent
## it from going to sleep.
##
## Author: Chris Pietschmann (https://build5nines.com)
$timerseconds = 60 * 4 ## Every 4 minutes
$myshell = New-Object -com "Wscript.Shell"
while ($True -eq $True) {
$myshell.sendkeys("{SCROLLLOCK 2}")
Write-Output "Keeping Awake... $(Get-Date)"
Start-Sleep -Seconds $timerseconds
}
@crpietschmann
Copy link
Author

Save this file some where and run it as needed:

./KeepAwake.ps1

@ealmonte32
Copy link

@crpietschmann

I use similar code but wondering what the "2" after ScrollLock means/is for? Is that the number of times the key is pressed? because I have been using the milliseconds timer to separate the command twice.

P.S. from what I know, a single $true in while ($true) will suffice.

@crpietschmann
Copy link
Author

Yup, the 2 is to "press" the Scroll Lock key twice. This will toggle it so that it triggers the computer to stay awake while basically keeping the state of the Scoll Lock key the same. So you don't have the Scoll Lock turning on and off every few minutes. It worked well for my usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment