Skip to content

Instantly share code, notes, and snippets.

@jamesfreeman959
Last active February 12, 2026 18:14
Show Gist options
  • Select an option

  • Save jamesfreeman959/231b068c3d1ed6557675f21c0e346a9c to your computer and use it in GitHub Desktop.

Select an option

Save jamesfreeman959/231b068c3d1ed6557675f21c0e346a9c 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 (1) {
# 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
}
@antroyd
Copy link

antroyd commented Feb 12, 2026

In general, the script runs beautiful. I had reduced it to the following oneliner, which even adds a random element:

$wsh=New-Object -ComObject WScript.Shell;$i=0;while(1) {$wsh.SendKeys('+{F15}');clear;write-host 'Caffeine applied for' $i seconds -nonewline;$j=(Get-Random -Minimum 5 -Maximum 25);$i=$i+$j;sleep $j}

The only problem is: It only doesn't run any more with PowerShell in ConstraintLanguage Mode, it appears this disables the function SendKeys. Does anyone have an alternate how to provide the function while ConstraintLanguage Mode is active?

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