Silently set the screensaver on Windows from the command line?

pupeno picture pupeno · Apr 12, 2018 · Viewed 15k times · Source

I know that if you run:

rundll32.exe desk.cpl,InstallScreenSaver toasters.scr

you can set the screensaver to toasters.scr but it also opens up the screensaver configuration dialog. Is there a way to set the screensaver on Windows without opening any dialog by running a command?

Answer

Maciej Pulikowski picture Maciej Pulikowski · Apr 24, 2018

I have found two ways to do it:

1) Add in registry, make sure is active and setTimeOut (only minutes)

CMD

reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v SCRNSAVE.EXE /t REG_SZ /d C:\Windows\System32\Mystify.scr /f
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveActive /t REG_SZ /d 1 /f
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveTimeOut /t REG_SZ /d 60 /f

JAVA

setScreenSaver(true, 1, "C:\\Windows\\System32\\Mystify.scr");

/**
 * set screen saver active, timeout and scr, only works in Windows
 * @param isActive
 * @param timeOutMin only minutes
 * @param pathToScr path to scr
 * @throws IOException
 */
public static void setScreenSaver(boolean isActive, int timeOutMin, String pathToScr) throws IOException{
    String _isActive = isActive ? "1" : "0";
    //only works with minutes, min. 1 min
    String _timeOut = timeOutMin > 1 ? timeOutMin*60+"" : "60";
    Runtime.getRuntime().exec(new String[] { "reg", "add", "HKEY_CURRENT_USER\\Control Panel\\Desktop", "/v", "SCRNSAVE.EXE", "/t", "REG_SZ", "/d", pathToScr,"/f" });
    Runtime.getRuntime().exec(new String[] { "reg", "add", "HKEY_CURRENT_USER\\Control Panel\\Desktop", "/v", "ScreenSaveActive", "/t", "REG_SZ", "/d", _isActive,"/f" });
    Runtime.getRuntime().exec(new String[] { "reg", "add", "HKEY_CURRENT_USER\\Control Panel\\Desktop", "/v", "ScreenSaveTimeOut", "/t", "REG_SZ", "/d", _timeOut,"/f" });
}

2) Get path from registry and rewrite scr file, but if is set to null, you can't do it.