Last active
September 7, 2021 14:16
-
-
Save taidalog/6fffd3bda36edaa32519e92788b1aa4c to your computer and use it in GitHub Desktop.
PowerShell script to lower monitor brightness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[string] | |
$TimeFrom = '17:00', | |
[string] | |
$TimeTo = '20:00' | |
) | |
Set-StrictMode -Version Latest | |
[DateTime]$datetimeFrom = Get-Date $TimeFrom | |
[DateTime]$datetimeTo = Get-Date $TimeTo | |
Write-Verbose "`$datetimeFrom.ToString() = $($datetimeFrom.ToString())" | |
Write-Verbose "`$datetimeTo.ToString() = $($datetimeTo.ToString())" | |
[int]$duration = ($datetimeTo - $datetimeFrom).TotalSeconds | |
Write-Verbose "`$duration = $($duration)" | |
$wmiMonitorBrightnessMethodsInstance = Get-CimInstance -Namespace root/wmi -ClassName WmiMonitorBrightnessMethods | |
[int]$timeout = 5 | |
[int]$waitSecond = 1 | |
while ($true) { | |
if ((Get-Date) -ge $datetimeFrom) { | |
[int]$passedSeconds = ((Get-Date) - $datetimeFrom).TotalSeconds | |
$wmiMonitorBrightnessInstance = Get-CimInstance -Namespace root/wmi -ClassName WmiMonitorBrightness | |
[int]$brightnessAsIs = $wmiMonitorBrightnessInstance.CurrentBrightness | |
[int]$brightnessToBe = [int](100 - ($passedSeconds / $duration * 100)) | |
Write-Verbose "`$passedSeconds = $($passedSeconds)" | |
Write-Verbose "`$brightnessAsIs = $($brightnessAsIs)" | |
Write-Verbose "`$brightnessToBe = $($brightnessToBe)" | |
if ($brightnessToBe -lt 0) { | |
$brightnessToBe = 0 | |
} | |
if ($brightnessAsIs -gt $brightnessToBe) { | |
$wmiMonitorBrightnessMethodsInstance | | |
Invoke-CimMethod -MethodName WmiSetBrightness -Arguments @{Brightness = $brightnessToBe; Timeout = $timeout;} | | |
Out-Null | |
} | |
} | |
Start-Sleep -Seconds $waitSecond | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment