Archive for June 29th, 2009

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

Advertisements

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  
Advertisements

%d bloggers like this: