Keep windows from locking

I have a script that I’m running as a logon task scheduler to move the mouse every 10 minutes because I want to keep windows from locking out after inactivity. I can see the task scheduler is running the task at logon and mouse is moved after 10 minutes.

Is it because if no one moves the mouse after the script is run, the mouse is just being moved at the same place? Would random mouse move work?

Or even open and close notepad every 10 minutes.

Add-Type -AssemblyName System.Windows.Forms

while ($true)
{
  $Pos = [System.Windows.Forms.Cursor]::Position
  $x = ($pos.X % 500) + 1
  $y = ($pos.Y % 500) + 1
  [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
  Start-Sleep -Seconds 600
}

There’s some discussion of this on StackOverflow. Essentially, just moving the cursor isn’t enough, Windows actually monitors for input.

This thread has a lot of (non-PowerShell) background, with a PowerShell solution at the end of the thread:

I am curious why you’d work around this programmatically rather than applying a separate policy to machines that should not lock.

1 Like

Thanks for the reply. I have a script that open and closes Notepad every 10 minutes, and so far its working. I had the script running since Friday and its still logged on.

I’m sure there might be a better way, but this should work for what I need.

I tried the Windows GPO to stop logging out of inactivity, but for some reason its not working. I can see the gpo when I run gpresults, but I guess there’s some other GP that’s over ruling it.

So you keep pushing your car instead of releasing the handbrake, getting in and driving off? :smirk: :wink: :stuck_out_tongue_winking_eye: :love_you_gesture:t4:

Yes, that’s true, need to figure it out. But found a solution for now. :slight_smile:

Would you like to share it here? It may help others having the same or a similar problem to solve. :wink:

Nothing fancy, I have this running as a task at logon. Opening and closing notepad every 10 minutes.

while ($true) {
    Start-Process notepad
    sleep -Seconds 5
    Stop-Process -Name notepad
    sleep -Seconds 600
}

A bit off topic, have you tried running RSOP to find the GPO that is being applied?

No I have not, I’ll look

Two comments:

  1. Your Stop-Process will exit all Notepad processes. Is this what you really want to do?
  2. You will have Notepad windows popping up, taking focus, and then disappearing after 5 seconds which would not be conducive to working

A far better solution would be to toggle ScrollLock on and then off again with your desired frequency. No windows popping open, not exiting processes that you actually want to run

@riedyw

  1. The users won’t be using notepad, but its not a good solution, I agree.
  2. Wouldn’t scrolllock toggle would be like moving the mouse every 10 minutes like I originally had?

From my experience it is difference because I have a batch file plus a Vbscript that toggles ScrollLock and it prevents my computer from locking. I’ve never translated it to Powershell as I have no need to.

Jiggle.cmd

@echo off
if "%1" == "" (
    start cmd.exe /c "color 27 & cscript.exe c:\scripts\ToggleScroll2.vbs 1 4 //nologo"
) else (
    start cmd.exe /c "color 27 & cscript c:\scripts\ToggleScroll2.vbs %1 4 //nologo"
)

ToggleScroll2.vbs

set WshShell = WScript.CreateObject("WScript.Shell")
dim varHour, varInterval
If WScript.Arguments.Count = 0 Then
    varHour = 1
    varInterval = 4
End If
If WScript.Arguments.Count = 1 Then
    varHour = CInt(WScript.Arguments(0))
    varInterval = 4
End If
If WScript.Arguments.Count >= 2 Then
    varHour = CInt(WScript.Arguments(0))
    varInterval = CInt(WScript.Arguments(1))
End If
StartTime = Now
EndTime = DateAdd("h", varHour, StartTime)
WScript.Echo "Beginning at " & StartTime & " estimated end time of: " & EndTime
WScript.Echo "This will run " & varHour & " hour(s) and interval is every " & varInterval & " minutes."
Do While Now < EndTime
    WScript.Echo "Toggling ScrollLock at: " & Now & " estimated end time of: " & EndTime
    WshShell.SendKeys "{SCROLLLOCK}"
    WScript.Sleep 100
    WshShell.SendKeys "{SCROLLLOCK}" 
    '); // Toggle Scroll Lock
    WScript.Echo "Sleeping for " & varInterval & " minutes. Press Ctrl-C to exit."
    WScript.Sleep (varInterval * 60 * 1000) 
Loop               ' or use this syntax
WScript.Echo "Ended at: " & Now
1 Like

Thanks for the blast from the past - VBScript - I wrote a TON of that stuff in a past life :slight_smile:

So far the PowerShell screenlock is working. Thanks for the idea.
I’ll go with that since the user won’t notice any difference.

I have to ask out of curiosity why you want the screen to not lock? The ScreenLock is a common/best practice from a security standpoint and it seems very quirky to have an endless loop running when you could just configure GPO?

Again, just curios.

Right, I agree the screen lock is a good practice for security. The reason is because these computers will be used to track the progress of work the employees are doing. I work for a manufacturing company. So it will be easier for the users and for me as IT if they don’t have to login every few minutes. Plus less chance of keyboard / mouse from getting dirty etc. Its a unique situation.

One reason more to do it proper and not with a quirky workaround. :wink:

These computers have no internet access, and only very specific areas to LAN, plus Sophos etc.
I know its not the best recommended method, but because of the nature of business, this is how they want it done.