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.