Setting Windows screensaver require password with powershell

Viewed 3073

I know that I can set screensaver values through the registry, but those apply at login. I gather the correct way is to use the SystemParametersInfo function, which then makes the changes apply immediately.

I am able to get and set the screensaver timeout values using the following:

$signature = @"
[DllImport("user32.dll")]
public static extern bool SystemParametersInfo(int uAction, int uParam, ref int lpvParam, int flags );
"@

$systemParamInfo = Add-Type -memberDefinition  $signature -Name ScreenSaver -passThru

Function Get-ScreenSaverTimeout
{
  [Int32]$value = 0
  $systemParamInfo::SystemParametersInfo(14, 0, [REF]$value, 0)
  $($value/60)
}

Function Set-ScreenSaverTimeout
{
  Param ([Int32]$value)
  $seconds = $value * 60
  [Int32]$nullVar = 0
  $systemParamInfo::SystemParametersInfo(15, $seconds, [REF]$nullVar, 2)
}

from https://powershellreflections.wordpress.com/2011/08/02/control-your-screensaver-with-powershell/

I am trying to change the flag for "On resume, display logon screen". Getting the value with the following is successful:

function Get-ScreenSaverSecure 
{
  [Int32]$value = 0
  $systemParamInfo::SystemParametersInfo(118, 0, [REF]$value, 0)
  $value
}

However, setting the value with:

Function Set-ScreenSaverSecure
{
  [Int32]$nullVar = 0
  $systemParamInfo::SystemParametersInfo(119, $true, [REF]$nullVar, 2)
}

does not set the flag. From the MSDN link, I think I should be passing SPI_SETSCREENSAVESECURE (which is uiParam 119) a $true or $false, depending on the mode, however neither seem to apply.

Any help is appreciated.

1 Answers
Related