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: Clear history of previous commands in PowerShell! 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: Kill a hanging process in Windows from the CMD! Read more →

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

  1. 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.

  2. 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
    
      }
  3. Hi All,
    I need a simple script like. No arrangement pass after 60 seconds laptop should get screen lock.

Leave a Reply