PoshCode Logo PowerShell Code Repository

Prevent-Screensaver (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1182"></script>download | new post

Simulate user activity to prevent desktop lock or screensaver for specified period of time

  1. #########################################################
  2. # Prevent-Screensaver
  3. #########################################################
  4. # This script "presses" a keyboard key every minute
  5. # for specified number of minutes which makes
  6. # Windows "think" you are at your desktop
  7. # so the screensaver does not start and the desktop
  8. # does not get locked.
  9. #########################################################
  10. # Usage:
  11. # & c:\filepath\Prevent-Screensaver.ps1 -Minutes 120
  12. # Makes the script press "." for 120 minutes.
  13. # Start notepad or another app and put focus there
  14. # to see the dots appear and prevent beeping
  15. ########################################################
  16. # (c) Dmitry Sotnikov
  17. # http://dmitrysotnikov.wordpress.com
  18. ########################################################
  19.  
  20. param($minutes = 60)
  21.  
  22. $myshell = New-Object -com "Wscript.Shell"
  23.  
  24. for ($i = 0; $i -lt $minutes; $i++) {
  25.   Start-Sleep -Seconds 60
  26.   $myshell.sendkeys(".")
  27. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me