Skip to content

Instantly share code, notes, and snippets.

@jamesfreeman959
Last active July 15, 2025 12:03
Show Gist options
  • Save jamesfreeman959/231b068c3d1ed6557675f21c0e346a9c to your computer and use it in GitHub Desktop.
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
}
@Jwboucher94
Copy link

If you don't want to save anything and need to bypass policy checks, you can just paste the code as a one-liner and run it in a powershell window (so no need for ISE)

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

and next time you open the shell, just tap arrow up on the keyboard and you can continue where you left off ;)

I've improved somewhat on this. This one liner will run until 4pm (technically, 16:00:59, but still) on the day it's run. Added the AddMinutes in case anyone wants more refined control over end time, though I don't actually use it.

$a = Get-Date; $b = ((Get-Date).Date).AddHours(16).AddMinutes(0); $wsh = New-Object -ComObject WScript.Shell; while ($a -lt $b) {$wsh.SendKeys('+{F15}'); Start-Sleep -seconds 59; $a = Get-Date}

Or, for non one-liner appearances, with some :

# Stop time
param([int]$hours=16)
param([int]$minutes=00)

$a = Get-Date
$b = ((Get-Date).Date).AddHours($hours).AddMinutes($minutes)
$wsh = New-Object -ComObject WScript.Shell
while ($a -lt $b) {
    $wsh.SendKeys('+{F15}')
    Start-Sleep -seconds 59
    $a = Get-Date
}

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