Last active
November 10, 2017 05:02
-
-
Save jay/2c4ca23314f1ed31c24da390d86c2b23 to your computer and use it in GitHub Desktop.
Set the AC & DC brightness in the current power scheme.
This file contains hidden or 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
:: Set the AC & DC brightness in the current power scheme. | |
:: | |
:: Usage: "setbrightness.bat" <brightness-level> | |
:: | |
:: Based on https://stackoverflow.com/a/26766798 | |
:: Also see https://github.com/HubKing/LinkBrightness | |
:: | |
:: Public Domain: No License. Written by Jay Satiro <[email protected]> | |
:: | |
:: https://gist.github.com/jay/2c4ca23314f1ed31c24da390d86c2b23 | |
:: | |
@echo off | |
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION | |
set GUID_DISPLAY=7516b95f-f776-4464-8c53-06167f40cc99 | |
set GUID_DISPLAY_BRIGHTNESS=aded5e82-b909-4619-9949-f5d71dac0bcb | |
rem Check that the requested brightness level is valid | |
echo "%~1"| findstr /r "^\"[0-9][0-9]*\"$" 1>NUL 2>&1 | |
if %ERRORLEVEL% NEQ 0 goto usage | |
if %~1 GTR 100 goto usage | |
set REQUESTED_BRIGHTNESS=%~1 | |
call :get_current_brightness | |
set PREVIOUS_BRIGHTNESS=%CURRENT_BRIGHTNESS% | |
rem Get the active power scheme | |
set GUID_ACTIVE_SCHEME= | |
for /f "tokens=2 delims=:" %%G in ('powercfg -getactivescheme') do set "GUID_ACTIVE_SCHEME=%%~G" | |
rem echo. & echo Active Power Scheme: "%GUID_ACTIVE_SCHEME% " | |
for /f %%G in ("%GUID_ACTIVE_SCHEME%") do set "GUID_ACTIVE_SCHEME=%%~G" | |
echo "%GUID_ACTIVE_SCHEME%"| findstr /r "^\"[0123456789abcdefABCDEF][0123456789abcdefABCDEF-]*\"$" 1>NUL 2>&1 | |
if %ERRORLEVEL% NEQ 0 echo Error: Could not parse GUID from active power scheme. & exit /b 1 | |
rem Adjusting current power scheme's AC & DC brightness settings. | |
rem Lines that start with logical-and operator must be indented to be parsed properly. | |
powercfg -setacvalueindex %GUID_ACTIVE_SCHEME% %GUID_DISPLAY% %GUID_DISPLAY_BRIGHTNESS% %REQUESTED_BRIGHTNESS% ^ | |
&& powercfg -setdcvalueindex %GUID_ACTIVE_SCHEME% %GUID_DISPLAY% %GUID_DISPLAY_BRIGHTNESS% %REQUESTED_BRIGHTNESS% ^ | |
&& powercfg -setactive %GUID_ACTIVE_SCHEME% | |
set RETCODE=%ERRORLEVEL% | |
if %RETCODE% NEQ 0 echo. & echo Error: powercfg failed, error %RETCODE% | |
call :get_current_brightness | |
if defined CURRENT_BRIGHTNESS ( | |
echo. | |
echo Active Monitor Brightness: %PREVIOUS_BRIGHTNESS%%% -^> %CURRENT_BRIGHTNESS%%% | |
if "%CURRENT_BRIGHTNESS%" NEQ "%REQUESTED_BRIGHTNESS%" ( | |
echo. | |
echo WARNING: Active monitor's current brightness level does not match set level. | |
rem For example when I set brightness 0 the system actually sets it to 5 | |
) | |
) else ( | |
echo. | |
echo WARNING: Active monitor's current brightness level unknown ^(not supported?^). | |
) | |
exit /b %RETCODE% | |
:get_current_brightness | |
rem Return the active monitor's brightness level in CURRENT_BRIGHTNESS. | |
rem If the level can't be determined then CURRENT_BRIGHTNESS is set empty. | |
set CURRENT_BRIGHTNESS= | |
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION | |
set WMI_CMDLINE= ^ | |
wmic /namespace:\\root\wmi path WmiMonitorBrightness ^ | |
WHERE "Active=TRUE" GET CurrentBrightness ^ | |
/format:list | |
for /f "usebackq tokens=2 delims==" %%i in (`%WMI_CMDLINE% 2^>NUL`) do ( | |
set CURRENT_BRIGHTNESS=%%i | |
) | |
rem export CURRENT_BRIGHTNESS back to caller | |
endlocal & set CURRENT_BRIGHTNESS=%CURRENT_BRIGHTNESS% | |
exit /b 0 | |
:usage | |
echo. | |
echo Usage: "%~n0%~x0" ^<brightness-level^> | |
echo Set the AC ^& DC brightness in the current power scheme to a level from 0 - 100. | |
call :get_current_brightness | |
if defined CURRENT_BRIGHTNESS echo. & echo Active Monitor Brightness: %CURRENT_BRIGHTNESS%%% | |
exit /b 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment