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: PowerShell, hack
Subscribe by email




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.
I did the same thing with AutoIt … however I just had the mouse move 1 pixel.
Yep, good comments. I love Jaykul’s stuff but wanted something simple and self-contained so I ended up with the keys approach.
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)?
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)
What happen when your program move the mouse enough times that the screen end, do yo get bips or what ?
Claus,
This is way-way better than what I originally used!
Learning something new every day. Thanks a lot for the tip!
Dmitry
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)