How can I keep a user session active with PowerShell?

0
4
Asked By CreativeSky109 On

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

Answered By KeyboardNinja82 On

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!

Answered By ServerWizard33 On

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.

TechieGuru45 -

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.

Answered By TechieGuru45 On

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!

PowerSharp99 -

I usually send F15 instead, as that key doesn't trigger anything in apps and keeps the session alive without any issues!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.