Skip to content

Instantly share code, notes, and snippets.

@taidalog
Last active September 7, 2021 14:16
Show Gist options
  • Save taidalog/6fffd3bda36edaa32519e92788b1aa4c to your computer and use it in GitHub Desktop.
Save taidalog/6fffd3bda36edaa32519e92788b1aa4c to your computer and use it in GitHub Desktop.
PowerShell script to lower monitor brightness
[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