Wednesday, July 18, 2007

Turning Off Screen Saver from Registry

I have found myself in a situation where I would like to have my screen saver deactivated.  I know, you can do it from the display property just fine, but sometimes I want to be able to do that from the command line so I can make that part of a setup script that I run for all my machines.

There are a couple of registry keys you need to tweak to turn it off.

HKCU\Control Panel\Desktop\ScreenSaveActive  :  "0"

HKCU\Control Panel\Desktop\SCRNSAVE.EXE      :  ""

The first one is the one that really matters.  You are basically turning it off just by setting that to 0.  But this will leave whatever default screensaver in the second registry, and that has the rather undesirable effect of showing that as the active screensaver if you open your display property.

Setting the second registry to an empty string basically just sets the active screensaver to none, which is consistent with how you would do it from the display property.

To actually set those values up in the command line, you can put the registry keys and values in a .reg file the way I did it in the Delayed Expansion article, or you can do it using the reg utility.

reg add "HKCU\Control Panel\Desktop" /v "ScreenSaveActive" /t REG_SZ /d "0" /f

reg add "HKCU\Control Panel\Desktop" /v "SCRNSAVE.EXE" /t REG_SZ /d "" /f

The /v switch specified the value (not to be confused with data), /t specifies the type of that value, and /d specifies the data you want to set the value to.  We also want to do /f so it does not prompt us when it sees that the values already exist.  Do a reg /? to find out more about the reg command.  And to check if they're indeed modified, you can use the reg query option.

reg query "HKCU\Control Panel\Desktop" /v "ScreenSaveActive" /t REG_SZ

reg query "HKCU\Control Panel\Desktop" /v "SCRNSAVE.EXE" /t REG_SZ

The meaning of the switches are as before, except this time we don't use the /d and /f option.  If you need to check if a particular value is set or not, you can simply pipe it to a findstr and see if it finds the value you are looking for.  For easiest result, you might want to use the findstr regular expression option.

reg query "HKCU\Control Panel\Desktop" /v "ScreenSaveActive" /t REG_SZ | findstr /R /C:"ScreenSaveActive.*0"

if "%errorlevel%" EQU "0" (

  echo Screensaver is off

) else (

  echo Screensaver is on


As you can see, reg is a pretty useful command to get and set registry keys and as long as you know what to change, it will get the job done for you.

No comments: