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
}
@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
}

@neuralpain
Copy link

@jamesfreeman959 How do you actually run this in a bat file? Does it work in windows 10?

I put this in a keepawake.bat file

powershell.exe -windowstyle hidden -file C:\keepawake.ps1 -Until 17:30

It doesn't seem to work though.

or is there a way to run it in PowerShell itself 😃

You can copy the powershell code to a .cmd file together with PowerBatch as shown below and run it.

<# :# DO NOT REMOVE THIS COMMENT
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set ARGS=%*
if defined ARGS set ARGS=%ARGS:"=\"%
if defined ARGS set ARGS=%ARGS:'=''%
PowerShell -c ^"$CMD_NAME='%~n0';Invoke-Expression ('^& {' + (get-content -raw '%~f0') + '} %ARGS%')"
exit /b
#>

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

exit

@MagusXL
Copy link

MagusXL commented Sep 21, 2025

I present to you the ultimate oneliner, StealthCoffee:

  • Creates a hidden window to run the new process
  • Autoexits the original window we paste this into
  • Stops running at 18:15 +/- 5 localtime of the same day
  • Sends scroll lock twice (random ms range 103-153) in random intervals between 33 seconds and 183 seconds

Repo to open in incognito and copy the oneliner from: StealthCoffee

Here is the snippet also, you can just copy paste it in a Powershell terminal:


Start-Process powershell -WindowStyle Hidden -ArgumentList '-Command "$wshell = New-Object -ComObject wscript.shell; $end = (Get-Date).Date.AddHours(18).AddMinutes(15).AddMinutes((Get-Random -Minimum -5 -Maximum 5)); while((Get-Date) -lt $end){$wshell.SendKeys(''{SCROLLLOCK}'' ); Start-Sleep -Milliseconds (Get-Random -Minimum 103 -Maximum 153); $wshell.SendKeys(''{SCROLLLOCK}''); Start-Sleep -Seconds (Get-Random -Minimum 33 -Maximum 183)}"'; exit


ProTip: Don't save it as a file locally. I don't have this script anywhere on my local machine to avoid any detection, i just open an incognito window in Google Chrome, navigate to the public repository i listed above and just copy paste the raw text from the file in a Powershell terminal.

@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