Prevent desktop lock or screensaver with PowerShell

Imagine that there’s a webcast that you absolutely need to record and your girlfriend calls because she had a bad dream and you need to go to give her consolation, or it’s your daughter’s birthday, or simply 11 pm because the Earth is huge and the timezones suck. Your first reaction is to simply try to record the webcast but this is a corporate PC and group policy is configured to lock the desktop after x minutes of inactivity. What do you do?

I found myself in this situation a few days ago, and did not want to search the internet and download a random executable simulating user activity and doing who knows what else on my computer.

Instead I wrote this simple PowerShell script:

param($minutes = 60)

$myshell = New-Object -com "Wscript.Shell"

for ($i = 0; $i -lt $minutes; $i++) {
  Start-Sleep -Seconds 60
  $myshell.sendkeys(".")
}

All the script does is creates a Windows scripting shell com object, and then for the specified number of minutes (which is a script parameter) every minute presses the “.” key.

Then I saved the script as Prevent-Screensaver.ps1 file (“prevent” is not a proper PowerShell verb, but disable- or stop- do not seem quite right…) and started it from PowerShell command-line: & c:\Prevent-Screensaver.ps1 120

One other thing which I also did was starting a notepad and clicking into it. This made the script output the dots into the application rather than overload Windows input buffer (which would have caused the OS to start beeping.)

Oh, and before anyone adds comments on how I have just ruined desktop security in the enterprise… By using this you might be circumventing security measures which your company might have for a reason. Check with your HR/IT/legal department/manager when in doubt.😉

[UPDATE] Check out what Claus posted in his comments here – an even better way of preventing the screensaver by moving the mouse cursor a bit.

Tags: ,

30 Responses to “Prevent desktop lock or screensaver with PowerShell”


  1. 1 stangm June 29, 2009 at 1:21 pm

    Using Jaykul’s WASP to send mouse clicks would help with the input buffer problem, and remove the need for launching notepad to accept the keys.

    Actually I did this a while back with a simple autoit script that I compiled as an .exe. So when I need this functionality I just run that program, and kill it when I’m done.

  2. 4 Dmitry Sotnikov June 29, 2009 at 1:36 pm

    Yep, good comments. I love Jaykul’s stuff but wanted something simple and self-contained so I ended up with the keys approach.

  3. 5 asf June 29, 2009 at 10:09 pm

    talk about using the wrong tool for the job, why is powershell even involved, just some excuse not to use a WSH script (I know its tagged a hack and you are a powershell guy, but still) The proper way to do this would be to use the windows api (You can call the native api with autoit, so using a key pressing hack there is even worse)

    And a “.”? why not toggle the start menu with ctrl+esc or something (print screen maybe if the scripting supports an escape code for that)?

  4. 6 Claus Thude Nielsen July 27, 2009 at 10:15 am

    I did something similar in AutoIt as well, just moving the mouse one “pixel”.

    So I thought I would try the same in PowerShell, here is what I ended up with, it will move the mouse one “Pixel” to the right, which is hardly noticeable.

    $Pos = [System.Windows.Forms.Cursor]::Position
    [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1) , $Pos.Y)

  5. 8 Dmitry Sotnikov July 28, 2009 at 3:16 pm

    Claus,

    This is way-way better than what I originally used!

    Learning something new every day. Thanks a lot for the tip!

    Dmitry

  6. 9 Dmitry Sotnikov September 8, 2009 at 7:38 pm

    My guess is that it will just stop at the border.

    However, you can just make it move to the side and back by doing something like:

    [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1) , $Pos.Y)
    [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($Pos.X , $Pos.Y)

    • 10 dj DeeJay October 23, 2015 at 1:37 am

      i tried the same but this is not preventing the screesaver from starting, so i landed on this page, but a keypress is also not good because it writes where the Focus is. Next I tried to consume the pressed key immedialtly but no chance😦

  7. 11 Henrik June 10, 2010 at 10:03 am

    My problem is that my screen saver starts even though Powershell is moving the mouse pointer around.

  8. 12 Mike November 18, 2010 at 3:37 pm

    “this is a corporate PC and group policy is configured to lock the desktop after x minutes of inactivity.”

    So how do you install PowerShell?

    • 13 Dmitry Sotnikov November 18, 2010 at 3:52 pm

      Comes standard on Win 7. But yes, I hear you: in some environments it might be not present and policies might prevent installation… Luckily in my situation this was not the case.

  9. 14 Delon Newman December 15, 2010 at 12:56 am

    Here’s something similar I’ve used in VBScript

    Dim WSHShell

    Set WSHShell = CreateObject(“WScript.Shell”)

    ‘ Press key every 1.5 minutes

    Do While True
    WSHShell.SendKeys “.”
    WScript.Sleep 90000
    Loop

  10. 15 Mike February 7, 2011 at 2:46 pm

    I know you are looking to avoid 3rd party tools, but this app has saved my life a thousand times, and would be perfect for what you’re doing It’s SO EASY!!

    http://www.autohotkey.com/docs/Tutorial.htm

  11. 16 müzso October 3, 2011 at 7:25 pm

    Combining Delon’s approach with the F15 keypress from Caffeine, I came up with this:

    Dim WshShell
    Set WshShell = WScript.CreateObject(“WScript.Shell”)
    Do While True
    WshShell.SendKeys(“{F15}”)
    WScript.Sleep(55000)
    Loop

    It works on Windows XP SP3 and Windows 2008 R2, so I assume it works on everything else in between (2003, 2008, Vista, 7). And the F15 keypress does not seem to have any side effects. Pasting the above script in a *.vbs file and putting it in my user’s Startup folder works for me like a charm.
    (Note: if you use this hack in a Terminal Service session and minimize the Terminal Service client’s window, the screen will get locked locked just as usual. I guess you cannot send keys in the session via SendKeys() if it’s minimized.)

    • 17 alien June 7, 2012 at 4:14 am

      Hey the scripts does work like a charm.Thank you!
      – just wondering which process to kill in task manager, in case I want to stop the script from working.

      • 18 Keith Trangmar December 7, 2012 at 10:12 am

        If you kill WSCRIPT.EXE that’s the Windows Scripting Host, which the process that’s running the script. If the script is running in a console window, the process might be called CSCRIPT.EXE instead.

    • 19 bjosephs February 18, 2015 at 8:43 am

      Sendkeys F15 on my system makes my num-lock toggle on and off.
      Win 8.1 pro x64
      Logitech K350 keyboard.

  12. 20 Vinay Jagadeesh Naikar June 12, 2013 at 5:07 am

    Hi. .. I have a script In which i run a ‘.exe’. This .exe waits for an input from user to log some events in a file. I want to develop an automated script that runs the .exe and also simulates the key press that will be an input to the .exe for it to continue. Can anybody help me. Thanks in advance.

  13. 21 sam September 23, 2013 at 3:15 pm

    Hi Dmitry,I used the code of claus for moving the mouse pointer.
    The following is the error am getting in my cmd prompt.Thanks in advance.

    Unable to find type [System.Windows.Forms.Cursor]: make sure that the assembly
    containing this type is loaded.
    At D:\myscript.ps1:1 char:37
    + $Pos = [System.Windows.Forms.Cursor] <<<< ::Position
    + CategoryInfo : InvalidOperation: (System.Windows.Forms.Cursor:S
    tring) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

    New-Object : Cannot find type [System.Drawing.Point]: make sure the assembly co
    ntaining this type is loaded.
    At D:\myscript.ps1:2 char:53
    + [System.Windows.Forms.Cursor]::Position = New-Object <<<< System.Drawing.Poi
    nt((($Pos.X) + 1) , $Pos.Y)
    + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentExcepti
    on
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewOb
    jectCommand.

    Please advise on how to load the assemblies.

    • 22 Dmitry Sotnikov September 24, 2013 at 6:37 am

      Which version of PowerShell are you using? 3.0, right? If so, see which architecture that is. Chances are that you are on x64 Windows and the assembly only exists for the architecture different from the one that you are using. Try the same code in regular PowerShell and PowerShell x86 and see whether one of these works.

  14. 23 Anonymous October 12, 2013 at 8:15 pm

    param($minutes = 60)

    $myshell = New-Object -com “Wscript.Shell”

    for ($i = 0; $i -lt $minutes; $i++) {
    Start-Sleep -Seconds 50
    $myshell.sendkeys(“{F15}”)
    }

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! above in C:\lock.ps1

    powershell.exe -windowstyle hidden -file C:\lock.ps1

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! above in .bat extension

    This works good for me in win 7 64bit thanks #Dmitry

  15. 24 nostrumuva June 6, 2014 at 2:44 pm

    There’s an android app called “Timeout Blocker” that vibrates at intervals you set and you can put your mouse on it. Says not to use it at work though🙂 https://play.google.com/store/apps/details?id=com.isomerprogramming.application.timeoutblocker&hl=en

  16. 25 cgsilver August 8, 2014 at 1:06 am

    old topic but thought to share experience in case some1 comes here:
    I confirm mouse position does not prevent screensaver or sleep (probably because mouse is actually not moved, just redrawn).
    However sendkey does prevent!
    I did not experience with f15 – I needed to see the script is/was working🙂 just start the script with notepad.exe, and you don’t have to open manually.
    also for the loop, why not timeless? my code:

    notepad.exe
    $myshell = New-Object -com “Wscript.Shell”
    $i=1
    while ($I -eq 1) {
    $myshell.sendkeys(“.”)
    start-sleep -Seconds 30
    }

    thanks for all
    cheers
    K

    • 26 Kirk October 3, 2014 at 11:05 am

      @cgsilver….sorry for the noob/basic question….I’m trying to use this script you posted but don’t exactly undertsand what to script it in, how to save it, and how to run it?

      I literally copied the scipt, pasted it into notepad, saved as “MouseMove.ps1” and that doesn’t seem to work!

      Can you give me a basic step 1-10 on how to copy, paste, save, and then run at start-up or Task Schedule.

      I have the same issue some of you all do. We have a PC at our Hospital that needs to never go to sleep, lock-screen, screensave, etc. and our Group Policy keeps reverting back to our standard 20min lock-screen setting (we can’t figure out why the setting won’t hold, it works for a day or so then breaks again). So, if this could keep the PC “awake/active” that would allow the PC to always be on until it reboots every 7am to refresh the Citrix session.

      Makes sense!?! Please help!!!!

  17. 27 Dara January 21, 2015 at 2:59 pm

    Great thread, Kirk, I don’t know if you got a resolution but copying and pasting the code, for me from IE9, gave me the wrong quotation marks; ” “.
    i had to change them manually in notepad after pasting. HTH

  18. 29 Anon February 6, 2015 at 2:48 am

    This was useful too….

    I harvested the ideas above and wrapped it into a re-usable function

    Function Pause-ScreenSaver
    {
    Param(
    [Bool]$Continue = $True, #If This Is True, The Function Will Run Forever
    [Int]$DefaultSleepTime = 60 #Time To Wait Before Re-Looping
    )

    #Create A New Shell Com Object
    $Shell = New-Object -COM “WScript.Shell”

    #While $Continue Is Tue, Loop
    While ($Continue -EQ $True)
    {
    $Shell.SendKeys(“F15”)
    Start-Sleep $DefaultSleepTime
    }
    }


  1. 1 When Five Lines Of Powershell Isn’t Enough | C O L I N . V A N N I E K E R K . M E Trackback on May 21, 2014 at 7:54 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




My Recent Tweets

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

June 2009
M T W T F S S
« May   Jul »
1234567
891011121314
15161718192021
22232425262728
2930  

%d bloggers like this: