Windows: Prevent Lock Screen Timeout When Idle

Corporate Windows computers and servers are usually configured to lock the screen after some time of inactivity.

This is especially inconvenient when your remote desktop session keeps disconnecting with “RDP Session Timeout” each time after few minutes of being idle.

In this article i will show how to stop computer from locking and keep the remote desktop session alive without any 3rd party tools and admin privileges, but with just 2 lines in PowerShell.

Cool Tip: Keyboard key press simulation using Arduino! Read more →

Prevent Lock Screen Timeout in Windows

To prevent Windows from locking when idle, to prevent termination of RDP session after timeout and to keep Skype for Business or any other messenger from going inactive, you can simulate key pressing with PowerShell.

Simply open the PowerShell and execute the following two lines of code:

$WShell = New-Object -Com Wscript.Shell
while (1) {$WShell.SendKeys("{SCROLLLOCK}"); sleep 60}

The first line creates a Windows scripting -Com Wscript.Shell object, while the second line creates an infinite loop during which it presses the SCROLLLOCK key (which seems to be useless nowadays) each minute.

These lines can be saved as a file with .ps1 extension, e.g. Disable-Screen-Lock.ps1, that can be executed with the right click and “Run with PowerShell“.

This PowerShell script will prevent the “RPD Session Timeout“, will stop your computer from locking, will disable screensaver and will make your Skype status always online.

Cool Tip: Clear history of previous commands in PowerShell! Read more →

8 Replies to “Windows: Prevent Lock Screen Timeout When Idle”

  1. Kelly Conrad says: Reply

    For dummies. How do I do this step by step? After I go to Windows Powershell and past the two lines of code:
    PS C:\Users\myname> $WShell = New-Object -Com Wscript.Shell
    >> while (1) {$WShell.SendKeys(“{SCROLLLOCK}”); sleep 60}
    What do I do next? I’m hoping this works. I’ tried installing apps such as Coddee and Coffee_FF and others, but by screen still locks up after 10 minutes. I’ve changed the Power and Sleep settings a hundred times but they default back to original settings.
    Thank you in advance!

    1. You save those 2 lines in a .ps1 file, e.g. “filename.ps1”. Launch powershell, point the prompt to the folder you saved the ps1 file to, and call it with “.\filename.ps1” … without the quotation marks.

      I’m not sure if running PowerShell as an administrator has an effect or not, but I did it and have had it running for nearly an hour and my session hasn’t been locked since I ran the script. I’m using it to avoid my Remote Desktop connection to my office computer being locked, so I don’t have to type in my password every 5, 10 minutes.

      1. Do I need to run the powershell script in my local machine or in the computer I am remotely connecting to?

  2. Did not work

  3. USE BELOW; It toggles scroll lock every 90 seconds with a countdown progress bar. Use it all the time, works to prevent inactivity on machine, skype.. etc.

    Clear-Host
    Write-Host "`n`n`n`n`n`n`n`ndun dun dun dun--Staying alive. staying alive"
    
    $wShell = New-Object -com "Wscript.Shell"
    
    while ($true){
    
    [int]$Time = 90
    $Length = $Time / 100
    
      For ($Time; $Time -gt 0; $Time--) {
        $min = [int](([string]($Time/60)).split('
        .')[0])
        $text = " " + $min + " minutes " + ($Time % 60) + " seconds"
      
    
       Write-Progress -Activity "Running..." -Status $Text -PercentComplete ($Time / $Length)
         Start-Sleep 1
      }
      $WShell.sendkeys("{SCROLLLOCK}")
        Start-Sleep -Milliseconds 200
      $WShell.sendkeys("{SCROLLLOCK}")
        Start-Sleep -Seconds $Time
    
      }
  4. Hi All,
    I need a simple script like. No arrangement pass after 60 seconds laptop should get screen lock.

  5. I have made a very simple and easy to use script(.bat) which disabled autolock by running the script in a console(cmd) window. If you want to stop the script, the user just need to close the console window.

    :: SAVE this script as a .bat file and execute it to disable autolock

    @echo off
    COLOR 0A
    mode 69,9
    title Stay Awake
    echo Dim objResult > %temp%\stay_awake.vbs
    echo Set objShell = WScript.CreateObject("WScript.Shell")    >> %temp%\stay_awake.vbs
    echo Do While True >> %temp%\stay_awake.vbs
    echo  objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}") >> %temp%\stay_awake.vbs
    echo  Wscript.Sleep (5000) >> %temp%\stay_awake.vbs
    echo Loop >> %temp%\stay_awake.vbs
    echo Start Time: %date% %time%
    ECHO Please close this window to stop the Stay_Awake Script
    echo.
    cscript %temp%\stay_awake.vbs
    
    1. Do I run this script in my local computer or the remote computer I am connecting to?

Leave a Reply