LoginSignup
0
0

More than 1 year has passed since last update.

Windowsでロック画面を表示したときだけディスプレイのスリープ時間を変更する

Posted at

普段、画面スリープ無効にしているけど、ロックしたときはスリープしてほしいと思っている人用の設定

内容

  • ロックしたとき、画面スリープ時間1分に設定
  • ロック解除したとき、画面スリープ時間0分(無効化)に設定

やっていることはタスクスケジューラーでpowercfgを呼び出すだけ。

設定スクリプト

管理者権限ありのpowershellで下記のコマンドを実行する。

$ErrorActionPreference = 'Stop'

$taskSessionStateChangeTrigger = Get-CimClass `
    -Namespace 'ROOT\Microsoft\Windows\TaskScheduler' `
    -ClassName 'MSFT_TaskSessionStateChangeTrigger'

# https://docs.microsoft.com/ja-jp/windows/win32/api/taskschd/ne-taskschd-task_session_state_change_type
$onLockTrigger = New-CimInstance `
    -CimClass $taskSessionStateChangeTrigger `
    -Property @{ StateChange = 7 } `
    -ClientOnly

$onUnlockTrigger = New-CimInstance `
    -CimClass $taskSessionStateChangeTrigger `
    -Property @{ StateChange = 8 } `
    -ClientOnly

Register-ScheduledTask `
    -TaskName 'ロック時にディスプレイのスリープ時間を短くする' `
    -Action (New-ScheduledTaskAction -Execute 'powercfg' -Argument '/change monitor-timeout-ac 1') `
    -Trigger $onLockTrigger `
    -Force `
    -User 'SYSTEM'

Register-ScheduledTask `
    -TaskName 'アンロック時にディスプレイのスリープ時間を戻す' `
    -Action (New-ScheduledTaskAction -Execute 'powercfg' -Argument '/change monitor-timeout-ac 0') `
    -Trigger $onUnlockTrigger `
    -Force `
    -User 'SYSTEM'

trap { break }

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0