Skip to content

Instantly share code, notes, and snippets.

@ErikPlachta
Last active June 1, 2024 17:11
Show Gist options
  • Save ErikPlachta/01030475999503e89abf4790dad65191 to your computer and use it in GitHub Desktop.
Save ErikPlachta/01030475999503e89abf4790dad65191 to your computer and use it in GitHub Desktop.
Using PowerShell to set the brightness of supported displays with parameters.
<#
.SYNOPSIS
Sets the brightness of supported displays to a specified level.
.DESCRIPTION
This function adjusts the brightness of the display using WMI (Windows Management Instrumentation). It works by invoking the WmiSetBrightness method on instances of the WmiMonitorBrightnessMethods class.
.PARAMETER BrightnessLevel
Specifies the brightness level to set. Acceptable values are integers ranging from 0 to 100.
.PARAMETER Immediate
Specifies whether the brightness adjustment should be applied immediately. The default is 0 (immediate). If not set to 0, specify the delay in seconds.
.INPUTS
None
This function does not accept piped input. It only uses parameters specified directly when the function is called.
.OUTPUTS
String
Outputs a string indicating the success of the operation with the set brightness level.
.EXAMPLE
Set-ScreenBrightness -BrightnessLevel 50
Sets the screen brightness to 50%.
.EXAMPLE
Set-ScreenBrightness -BrightnessLevel 30 -Immediate 1
Sets the screen brightness to 30% with a delay.
.NOTES
This function requires administrative privileges to execute successfully. It may not work on all hardware as some displays do not support brightness control via WMI.
Make sure to run this function in a PowerShell session with administrative rights to avoid permission issues.
.LINK
https://gist.github.com/ErikPlachta/01030475999503e89abf4790dad65191
#>
function Set-ScreenBrightness {
param(
[int]$BrightnessLevel, # Brightness level to set (range: 0–100)
[int]$Immediate = 0 # Set to 0 for immediate adjustment, otherwise specify delay in seconds
)
try {
# Retrieve all instances of the WmiMonitorBrightnessMethods class from the WMI namespace
# Use -ErrorAction SilentlyContinue to suppress default error messages
$brightnessInstances = Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods -ErrorAction SilentlyContinue
if ($brightnessInstances -eq $null) {
# If no instances are found, throw a custom error
throw "No brightness control instances found. Your hardware may not support this feature or the necessary drivers are not installed."
}
# Loop through each instance found (usually one per monitor)
foreach ($instance in $brightnessInstances) {
# Invoke the WmiSetBrightness method on each instance
$instance | Invoke-CimMethod -MethodName WmiSetBrightness -Arguments @{Brightness = $BrightnessLevel; Timeout = $Immediate}
}
Write-Output "Brightness set to $BrightnessLevel%."
} catch [Microsoft.Management.Infrastructure.CimException] {
# Handle specific CIM exceptions with a custom message
Write-Error "Brightness control is not supported by your hardware or the necessary drivers are not installed."
} catch {
# Handle general exceptions
Write-Error "An unexpected error occurred: $_"
}
}
# Example usage: Set the screen brightness to 50%
#Set-ScreenBrightness -BrightnessLevel 50
# To retrieve help information on this function:
#Get-Help Set-ScreenBrightness -Full
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment