# Script to change screensaver time on Windows

A screensaver is set to turn on after a period of inactivity of the user. It is important to protect your devices when unattended as it is vulnerable to potential intruders. A screensaver prevents someone from viewing your personal files and can also be used as a medium to display your brand when idle. You can set a time delay for inactivity before the preset screensaver kicks in with Hexnode’s [custom script](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) feature.

 Note: 
**Supported Versions**:
The scripts given below will be supported on the following versions:

- Windows 10 v1607+ (Pro, Enterprise, Education)
- Windows 11 (Pro, Enterprise, Education)

 

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

PowerShell script 
------------------

Script to Change Screensaver Time on Windows

param( \[int\]$timeLimit ) try { if($timeLimit) { "changing machine inactivity limit to "+ $timeLimit + " seconds" $path = 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\\' $name = 'InactivityTimeoutSecs' Set-ItemProperty -Path $path -Name $name -Value $timeLimit "-----inactivity limit successfully updated-----" } else { "-----------please provide machine inactivity limit as argument (in seconds)-----------" } } catch { Write-Output $\_.Exception.Message } 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

  param(

 \[int\]$timeLimit

)

try

{

 if($timeLimit)

 {

 "changing machine inactivity limit to "+ $timeLimit + " seconds"

 $path = 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\\' 

 $name = 'InactivityTimeoutSecs'

 Set-ItemProperty -Path $path -Name $name -Value $timeLimit

 "-----inactivity limit successfully updated-----"

 }

 else

 {

 "-----------please provide machine inactivity limit as argument (in seconds)-----------"

 }

}

catch {

 Write-Output $\_.Exception.Message

} 

   

 

  

The script takes input `$timeLimit` in seconds and modifies the wait time for the screensaver already set by the user on the device to take effect.

The script works by modifying the registry to change `InactivityTimeoutSecs` at the path `HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\`.

The time delay is set in seconds (1 to 60) in the **Arguments** field. If the argument field is either left empty or set to a value of 0, the script returns an output to enter the inactivity limit in seconds as an argument.

Batch script 
-------------

Batch script to Change Screensaver Time on Windows

reg add "HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System" /v InactivityTimeoutSecs /t REG\_DWORD /d <time> /f 

   1

  reg add "HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System" /v InactivityTimeoutSecs /t REG\_DWORD /d <time> /f 

   

 

  

The `reg add` command will create a REG\_DWORD under the location HKEY\_LOCAL\_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System. Replace `<time>` with an integer value ranging from 1 to 60, indicating the wait time (in seconds) for the screensaver on the device to take effect.

Alternatively, you can use the same script after replacing `<time>` with 0 to restore the screensaver time to its default setting.

For example:

`reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v InactivityTimeoutSecs /t REG_dWORD /d 60 /f `

 Notes:- The restriction will take effect after the device restarts. You can use the [Restart Device](https://www.hexnode.com/mobile-device-management/help/restart-a-device-using-hexnode-mdm/) action to remotely initiate a device restart.
- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.