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 →

Was it useful? Share this post with the world!

19 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
    
      }
    1. This worked like a charm to keep my Microsoft Remote Desktop on a macbook pro from logging out, and it keeps the mac itself from falling asleep also.

      Just keep it at the top focus when stepping away from the machine/MRD.

      I normally would use caffeinate -DM command line in Terminal on mac to accomplish similar on the mac side, but with this, it keeps the PC side and the Mac side from logging out of the corporate issued machine they’re both on.

      Very excellent.

  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?

      1. Lookup Invoke-Command

        it will help you accomplish what you are looking to do.

  6. To make the Op’s script work correctly, you need to add quotes around “WScript.Shell” otherwise you will get an error “Value does not fall within the expected range.”

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

  7. You saved my life, thanksssss !!!

  8. Is there a way to have a script like this send a keystroke to a specific window? The problem is some web based tools time out when the window is inactive and then I have to log in again, which can get annoying as it might happen many times a day. Is there a script that will send a keystroke to a specific window as if it were the active window, so that as long as the window is open somewhere, even in the background, I won’t get logged out? Thanks.

    1. I used this as a basis for my scripts to send keystrokes to specific programs so it may help you.
      https://www.vbsedit.com/html/2b9476ce-54a7-4a00-b761-25bf9f36e83f.asp

  9. Steven, Did you get an answer to your question?

  10. Any way this can be set to a timer? I’d like this function to work, then stop after 30mins. . .

  11. How do you end the script to resume the lock screen timeout?

    1. Click START (desktop), then POWER OFF

  12. Here is a one-liner version (no need for a PS1 file 😉 ) which does not change the actual scrolllock status by sending the key sequence twice, displaying the datetime to show it is running:

    while (1) {(New-Object -Com "Wscript.Shell").SendKeys("{SCROLLLOCK}{SCROLLLOCK}");Get-Date; sleep 60}
    

Leave a Reply