Skip to content

Instantly share code, notes, and snippets.

@Skhmt
Last active April 1, 2024 19:53
Show Gist options
  • Save Skhmt/c6d1809a0625aa4adb28169ae82436bb to your computer and use it in GitHub Desktop.
Save Skhmt/c6d1809a0625aa4adb28169ae82436bb to your computer and use it in GitHub Desktop.
Prevent windows from sleeping

Keeping Windows awake without third party programs

Some options based on what permissions your admins allow

Powershell

Paste the following directly in PowerShell or put into PowerShell ISE then highlight the line and click "Run Selection (F8)" to run even if running scripts is disabled via Execution Policy.

$wsh = New-Object -COMObject WScript.Shell; while (1) {$RND = Get-Random -Minimum 30 -Maximum 120; $wsh.SendKeys('+{F15}'); Write-Host "$(Get-Date)"; Start-Sleep -Seconds $RND}

Does not work in ConstrainedLanguageMode - you can check with: $ExecutionContext.SessionState.LanguageMode

You don't need an F15 key on your physical keyboard for this to work.

Re: https://gist.github.com/jamesfreeman959/231b068c3d1ed6557675f21c0e346a9c

.bat with JScript

Create a .bat file with the following in it, then just run it:

@if (@a==@a) @end /*

@echo off

set SendKeys=CScript //nologo //E:JScript "%~F0"

set starttime=%TIME%
set startdate=%DATE%

:loop

cls
set /a rng = %RANDOM% * 60 / 32768 + 30
echo Sending [Shift]+[F15]
echo Start:  %starttime% %startdate%
echo Latest: %TIME% %DATE%
%SendKeys% "+{F15}"
timeout /t %rng% /nobreak

goto loop

*/

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

This is kind of crazy; it uses JScript conditional compilation inside of a file that can be read as both a .js and .bat. It basically does the same thing, sending Shift+F15 every 30 seconds.

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