How Can I Change the Screensaver Timeout Value?

How Can I Change the Screensaver Timeout Value?

  • Comments 6
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! Can I use a script to change the timeout value for the screensaver on a computer?

-- JN

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JN. For some reason, Microsoft’s scripting technologies come up a little short any time you’re talking about Windows settings and components, things like screensavers, wallpaper, the Taskbar, the Start menu, etc. You can use WMI (in particular, the Win32_Desktop class) to read many of these values, but you can’t use the Win32_Desktop class (or any equivalent class or object) to modify these values. Why? To be honest, we don’t really know; that’s just the way it is.

Fortunately, though, many of these settings are stored in the Windows registry, and if it’s stored in the registry, there’s a good chance you can write a script to manage those settings. The screensaver is no exception. Open up Regedit, and take a look in HKEY_CURRENT_USER; in the Control Panel\Desktop key, you’ll see several registry values related to the screensaver, including ScreenSaveTimeout. As you might have guessed, this is the registry value that determines how long the system must be idle before the screensaver kicks in. By default, this is set to 600 seconds, or 10 minutes.

So how do you change that value? By using a script just like this one, which changes the timeout value to 5 minutes (300 seconds):

HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Control Panel\Desktop"
objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
ValueName = "ScreenSaveTimeout"
strValue = "300"
objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue

To do this, we start by creating a constant named HKEY_CURRENT_USER and set the value to &H80000001; this tells WMI’s Registry provider that we’re working with the HKEY_CURRENT_USER portion of the registry. If we wanted to work with HKEY_LOCAL_MACHINE, we’d set a constant equal to &H80000001; &H80000003 would let us work with HKEY_USERS.

For this script we’re working on the local computer, so we set the variable strComputer to a dot (a WMI shortcut meaning the local computer). But the great thing about the Registry provider is that it works just as well on remote computers as it does the local computer. If you want to change the screensaver timeout value on a remote computer, just set the value of strComputer to the name of that computer.

From there we connect to the WMI service, and specify the registry path (Control Panel\Desktop). Next we call the CreateKey method. This is just to be on the safe side. If the registry key we’re looking for doesn’t exist, CreateKey will create it. If the registry key already exists, then CreateKey will just leave well enough alone.

After specifying the name of the registry value we want to change (ScreenSaveTimeout) and the new value (300), we then use the SetStringValue method to change the timeout value to 300 seconds.

Not too bad, huh? For more information about the Registry provider - probably one of the most under-appreciated on all WMI providers - take a look at the Registry chapter in the Microsoft Windows 2000 Scripting Guide. And check out the infamous Tweakomatic, the amazing utility that will actually write scripts that can read from and write to the registry. And to steal from an old commercial, with a name like Tweakomatic, it has to be good.

Incidentally, here’s another useful screensaver script for you; this one ensures that the screensaver is password-protected:

HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Control Panel\Desktop"
objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
ValueName = "ScreenSaverIsSecure"
strValue = "1"
objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue

See how much fun registry scripting can be?!? The Tweakomatic has additional scripts for managing the screensaver, as well as scripts for managing the Start menu, the Task bar, Windows Explorer folder settings, and a whole lot more. If you’re looking for ways to manage common desktop settings, that’s as good a place to start as any.


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>I understand you just change the value of strComputer to access the registry of another computer, but in this case, the script must alter a value from HKEY CURRENT USER. But the intended CURRENT USER is the one logged on the remote computer, not my own. What can I do to change a value from the HKEY CURRENT USER in the remote machine? Let's say, the user logged on at the moment I execute the script.</p>

  • <p>I tried running this script in PS ISE as a PS1 and I keep getting the &quot;Ampersand not allowed. The &amp; operator is reserved for future use; use &quot;&amp;&quot; to pass ampersand as a string.&quot;-error.</p> <p>Pointer is to the first use of the ampersand (&amp;H80000001) and if I comment out this line, I get the error when using the ampersand to concatenate the GetObject parameter.</p> <p>I am suspecting that PS needs som add-ins/snap-ins or other daemon-tasks associated?</p> <p>I am running Win7 and have administrator rights. PS run as administrator. I have not been able to find any help when googling the error.</p> <p>I am not a trained programmer and new to scripting but I have some experience in programming office-applications using VBA.</p>

  • <p>@Adriano Pinaffo -</p> <p>You would need to somehow cause the script to run within the context of the currently logged in user. Perhaps create a scheduled task, or run the script as part of a logon script. The best way to configure this setting is to use Group Policy. </p> <p>@UHJ-</p> <p>The code shown here is VBScript code. See this Hey Scripting Guy article for ideas about accomplishing this via Windows PowerShell <a rel="nofollow" target="_new" href="http://bit.ly/dUkXNc">http://bit.ly/dUkXNc</a> </p>

  • <p>Confused as &amp;H80000001 can&#39;t refer to both HKEY_CURRENT_USER &nbsp;and HKEY_LOCAL_MACHINE, can it?</p>

  • <p>Nope, &amp;H80000001 refers to HKCU. &amp;H80000002 refers to HKLM. Must be a mistype.</p>

  • <p>If I change a value how can I _activate_ the changed setting?</p> <p>Like setting the ScreenSaveTimeOut REG_SZ how can I make Windows actually use the new setting without logging off or rebooting?</p>