Silently set the screensaver on Windows from the command line?

32.2k Views Asked by At

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?

4

There are 4 best solutions below

6
Maciej Pulikowski On BEST ANSWER

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.

0
Tarun Lalwani On

Instead of running that command you should just run the command

reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v SCRNSAVE.EXE /t REG_SZ /d C:\Windows\system32\toasters.scr /f

This will update the screensaver

2
Soleil On

The modern way, with powershell

Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name ScreenSaveActive -Value 1
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name ScreenSaveTimeOut -Value 60
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name scrnsave.exe -Value "c:\windows\system32\mystify.scr"

You can put these in a ScrnInstaller.ps1 script that you execute with the command:

$ powershell -WindowStyle hidden -f "ScrnInstaller.ps1"

NB: Thoses parameters are superseeded by group policy parameters (eg., to force a screen saver for users in an enterprise). You have several ways to force it here.

With user / domain / site awareness: group policy

Using powershell and group policy, you can manage for which Organizational Unit / Domain / Site you're affecting the change, and it's having prevedence over user settings.

Changing group policy in the case of the screen saver time out:

Get-Command -Module GroupPolicy
New-GPO -Name "ScreenSaverTimeOut" -Comment "Sets the time to 900 seconds"
Set-GPRegistryValue -Name "ScreenSaverTimeOut" -Key "HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop" -ValueName ScreenSaveTimeOut -Type DWord -Value 900
New-GPLink -Name "ScreenSaverTimeOut" -Target "ou=MyOU,dc=myenterprise,dc=com"
gpupdate /force /target:computer

for myenterprise.com. For New-GPLink parameters: msdn reference

Then you can review your GP:

Get-GPO -Name "ScreenSaverTimeOut" | Get-GPOReport -ReportType HTML -Path $Home\report.html
Invoke-Item $Home\report.html
0
Ernie Nazario On
c:\Windows\System32\scrnsave.scr -start

The above live in a batch file will start the blank screen saver. Pros; Replacing scrnsave.scr with one of the other 5 screensavers in C:\Windows\system32\ will work just as well. You will not need to restart your system and it will start right up. Cons; I don't know how to set the timeout. I suspect there would be an argument for the line like c:\Windows\System32\scrnsave.scr /ScreenSaveTimeOut = 150 -start I think there needs to be a separate batch file to stop the screen saver.