Category filter
Script to set Screensaver on Windows
A screensaver is set to turn on after a period of inactivity of the user. It can be used as a medium to display your brand when the screen is idle. It is also used to prevent others from viewing personal content when the user is away for a short while. You can set a screensaver on your Windows devices remotely using Hexnode’s custom script feature.
Script to fetch SID
To change the screensaver, we need to specify the user’s security identifier (SID), or we should change the screensaver for every user on that device. To get the SID of any user, run the command:
1 |
Get-WmiObject win32_useraccount | Select-Object Name,SID |
Get-WmiObject
-gets instances of WMI classes or information about the available WMI classes.
Select-Object
-selects specified properties of an object or set of objects.
Script to set screensaver
Once the SID of a user is known, the screensaver of a specific user can be changed by providing their SID. Run the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
$userSID = $args[0] $filePath = "C:\Windows\system32\scrnsave.scr" Write-Host "--------------starting script execution--------------" try{ $status = New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS Function Set-Screensaver($sid) { if(Test-Path "HKU:\${sid}") { Write-Host "Changing screensaver for user with sid:",$sid Set-ItemProperty -Path "HKU:\${sid}\Control Panel\Desktop" -Name ScreenSaveActive -Value 1 Set-ItemProperty -Path "HKU:\${sid}\Control Panel\Desktop" -Name ScreenSaveTimeOut -Value 60 Set-ItemProperty -Path "HKU:\${sid}\Control Panel\Desktop" -Name scrnsave.exe -Value $filePath } } if(!$userSID){ $userDetails=Get-wmiobject win32_useraccount | where-object{$_.status -eq 'ok'} foreach($user in $userDetails){ $sid=$user.SID Set-Screensaver($sid) } } else{ Set-Screensaver($userSID) } } catch { Write-Host "Error occured while running script -> ",$_.Exception.Message } Write-Host "--------------script execution completed successfully--------------" |
If the SID of the user is specified as an argument, then the screensaver will be set only for that specific user. The script accepts an argument $userSID
to change the screensaver of the corresponding user.
If no arguments are passed in place of SID, screensavers will be set for all the users in the device.