Skip to content

Instantly share code, notes, and snippets.

@Rican7
Forked from jamesfreeman959/keepawake.ps1
Last active January 25, 2021 10:50
Show Gist options
  • Save Rican7/b0f01effc5a45d04a95013bc9532820c to your computer and use it in GitHub Desktop.
Save Rican7/b0f01effc5a45d04a95013bc9532820c to your computer and use it in GitHub Desktop.
KeepAwake - Keep Windows "awake" - A PowerShell script to keep Windows awake and make think the user is active on the keyboard
# KeepAwake - Keep Windows "awake"
#
# Useful for keeping awake during the running long/maintenance processes without
# having to change power or screen-saver options.
#
# See https://gist.github.com/jamesfreeman959/231b068c3d1ed6557675f21c0e346a9c
Write-Host @"
KeepAwake running...
Windows will stay awake as long as this is running.
(send Ctrl+C to quit)
"@
# Useful references:
#
# https://superuser.com/questions/992511/emulate-a-keyboard-button-via-the-command-line
# https://ss64.com/vb/sendkeys.html
# https://social.technet.microsoft.com/Forums/windowsserver/en-US/96b339e2-e9da-4802-a66d-be619aeb21ac/execute-function-one-time-in-every-10-mins-in-windows-powershell?forum=winserverpowershell
# https://learn-powershell.net/2013/02/08/powershell-and-events-object-events/
#
# Future enhancements - use events rather than an infinite loop
while (1) {
$wsh = New-Object -ComObject WScript.Shell
# Send Shift+F15 - this is the least intrusive key combination I can think of and is also used as default by:
# http://www.zhornsoftware.co.uk/caffeine/
# Unfortunately the above triggers a malware alert on Sophos so I needed to find a native solution - hence this script...
$wsh.SendKeys('+{F15}')
Start-Sleep -seconds 59
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment