Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
@mnotgninnep
Copy link

mnotgninnep commented Jun 9, 2021

Thank you for this. Please put the line "$wsh = New-Object -ComObject WScript.Shell" before the while loop. You don't need to call it every time it loops.

@jamesfreeman959
Copy link
Author

jamesfreeman959 commented Jun 11, 2021

Good catch!

Thank you for this. Please put the line "$wsh = New-Object -ComObject WScript.Shell" before the while loop. You don't need to call it every time it loops.

I've updated the code. I've been playing with moving the mouse instead of pressing a key as even the F15 keypress results in some odd artefacts in Linux terminal sessions. However having mixed results with this. If you want to experiment this with, try the following inside the loop:

$Pos = [System.Windows.Forms.Cursor]::Position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1), $Pos.Y)

Over time this would cause the cursor to creep towards the edge of the screen one pixel at a time, so you might want to move it, then, then move it back a pixel. However I am not sure this is keeping my system alive so please have a play if you're interested!

@OraDotNetDev
Copy link

OraDotNetDev commented Apr 12, 2022

This StackoverOverflow Question tried the same logic calling [Windows.Forms.Cursor], without success, they ended up using Python instead.

Good catch!

Thank you for this. Please put the line "$wsh = New-Object -ComObject WScript.Shell" before the while loop. You don't need to call it every time it loops.

I've updated the code. I've been playing with moving the mouse instead of pressing a key as even the F15 keypress results in some odd artefacts in Linux terminal sessions. However having mixed results with this. If you want to experiment this with, try the following inside the loop:

$Pos = [System.Windows.Forms.Cursor]::Position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1), $Pos.Y)

Over time this would cause the cursor to creep towards the edge of the screen one pixel at a time, so you might want to move it, then, then move it back a pixel. However I am not sure this is keeping my system alive so please have a play if you're interested!

@ice423cube
Copy link

ice423cube commented Aug 29, 2022

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

@ice423cube
Copy link

ice423cube commented Aug 29, 2022

@jamesfreeman959 FYI. If anyone else comes a cross this and needs to know how to use it 😃. All you need to do is copy the above code to your computer and put in a "keepawake.ps1" file. If you open it in nope pad you can paste in the code. Then create a "keepawake.bat" file on your computer and open in notepad and add the following "PowerShell -file C:\keepawake.ps1". It will leave a command window open. If you ever want to stop it all you need to do is open the command window and hit the control key and the c key at the same time "ctrl+c". That kills the command. It is that simple 😃. Hope that helps.

@jamesfreeman959
Copy link
Author

jamesfreeman959 commented Aug 29, 2022

Thanks for the update - I'm sure many will find that helpful! One little tip I picked up - I've had Windows machines where Group Policy prevents PowerShell scripts from being run. Yet if you open the ISE, copy the code in (do not save it - this triggers the policy - it has to run directly in the ISE) - it'll run quite happily. Naturally you have the PowerShell ISE open whilst it's running but I find that's a small price to pay.

@YouSpoonyBard
Copy link

YouSpoonyBard commented Aug 29, 2022

@jamesfreeman959 You can also take the ps1 file, right click> run with PowerShell. It opens up a PowerShell window, and you can close the window when you're done with the program.

@tsuke35
Copy link

tsuke35 commented Sep 23, 2022

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 ;)

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