I'm looking for a quick PowerShell script that can simulate keyboard keypresses or mouse movements to keep a user session active and prevent it from being locked due to inactivity. I've tried using the following code:
$myshell = New-Object -ComObject "WScript.Shell"
$myshell.SendKeys("{F12}")
However, this only seems to work at the application level and doesn't prevent the system from locking the session. What other methods can I use?
3 Answers
One funny solution is to buy one of those battery-powered cat toys and tape it to your mouse. That might keep the cursor moving enough to prevent the lock screen from coming up!
Have you considered using Caffeine, the application that prevents the computer from going to sleep? It might be a simpler solution if you don’t mind using additional software.
Instead of sending F12, try sending a "scroll lock" command. Also, you might want to set a timeout - I used to resend the command every 60 seconds. That helps too!
I usually send F15 instead, as that key doesn't trigger anything in apps and keeps the session alive without any issues!
I can't use that right now. The context is that I'm provisioning a Server2025 template with Packer and FirstLogonCommands, and it's taking longer than the inactivity limit allows.