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: ,

8 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. 3 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. 4 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. 5 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. 7 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. 8 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)


Leave a Reply




View Dmitry Sotnikov's profile on LinkedIn

Follow Dmitry Sotnikov at Twitter

My Recent Tweets

Archives

See you at:

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 Quest Software or anyone else for that matter. All trademarks acknowledged.

© 2007 Dmitry Sotnikov

Pages

 

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