Skip to content

Instantly share code, notes, and snippets.

@mrjohannchang
Forked from jamesfreeman959/keepawake.ps1
Last active March 13, 2023 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjohannchang/f8581ac12c4d23836bd081f9dbcde49d to your computer and use it in GitHub Desktop.
Save mrjohannchang/f8581ac12c4d23836bd081f9dbcde49d to your computer and use it in GitHub Desktop.
A very simple PowerShell script to keep a Windows PC awake and make lync think the user is active on the keyboard
# 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
$wsh = New-Object -ComObject WScript.Shell
while ($true) {
# 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
}
@mrjohannchang
Copy link
Author

mrjohannchang commented Feb 13, 2023

One-liner

while ($true) { $(New-Object -ComObject WScript.Shell).SendKeys('+{F15}'); Start-Sleep -seconds 59 }

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