Use PowerShell to Report and Set Monitor Brightness

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to report and set monitor brightness.

Microsoft Scripting Guy, Ed Wilson, is here. Today I was exploring the Root/WMI namespace in Windows Management Instrumentation (WMI) on my Windows 8 laptop. I happened to run across a few WMI classes that I had not messed around with before. I like to use the Get-CimClass cmdlet from Windows PowerShell 3.0 for this kind of explorations because it supports tab completion and other features that make this exploration easy.

Look for WMI classes related to monitor brightness

So I was initially looking for anything related to monitors. And as I looked at what scrolled by, I saw that there were classes related to monitor brightness. So I decided to modify my query a bit to include things related to monitor brightness. I derived the following command, and achieved the results shown here:

PS C:> Get-CimClass -Namespace root/WMI -ClassName *monitorbrightness*

 

   NameSpace: ROOT/wmi

 

CimClassName                        CimClassMethods      CimClassProperties

————                        —————      ——————

WmiMonitorBrightnessEvent           {}                   {SECURITY_DESCRIPTOR, TI…

WmiMonitorBrightness                {}                   {Active, CurrentBrightne…

WmiMonitorBrightnessMethods         {WmiSetBrightness… {Active, InstanceName}

I thought I would try to query the WmiMonitorBrightness WMI class first because it looks like it would report first. So I edited my previous line and changed from Get-CimClass to Get-CimInstance. Here is the command, and the output associated with the command:

PS C:> Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness

 

Active            : True

CurrentBrightness : 100

InstanceName      : DISPLAYLEN40B24&3062e51&0&UID67568640_0

Level             : {0, 1, 2, 3…}

Levels            : 101

PSComputerName    :

So without any reference material, I can see that the current brightness of my laptop monitor is 100. This is probably a percentage setting. I connect to MSDN, and look up WmiMonitorBrightness class. Sure enough it is the current brightness of the monitor as a percentage. The Level property lists supported values for CurrentBrightness. I check it by piping the results to the Select-Object cmdlet. Here is a sample of the output:

PS C:> Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness | select

-ExpandProperty level

0

1

2

3

4

<truncated>

The strange thing is that Level lists how many different levels are available. The value of 101 makes sense because Level begins with 0 and ends with 100.

Set the brightness

To set the brightness on my laptop display monitor, I figure I can use the WmiMonitorBrightNessMethods WMI class. While I have MSDN open, I check out WmiMonitorBrightnessMethods class, and it appears that I will be able to use it to set my monitor brightness:

PS C:> Get-CimClass -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods

   NameSpace: ROOT/WMI

 

CimClassName                        CimClassMethods      CimClassProperties

————                        —————      ——————

WmiMonitorBrightnessMethods         {WmiSetBrightness… {Active, InstanceName}

There are only two properties: Active and InstanceName. I decide to look at them. Here is the result:

PS C:> Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods

                      Active InstanceName                PSComputerName

                      —— ————                ————–

                        True DISPLAYLEN40B24&3062e5…

Examine the WMI class methods

So, the WmiMonitorBrightnessMethods WMI class looks like it might be useful. Now, I want to look at the method information in more detail:

PS C:> $wmi.CimClassMethods

 

Name                            ReturnType Parameters           Qualifiers

—-                            ———- ———-           ———-

WmiSetBrightness                   Boolean {Brightness, Time… {Implemented, Wmi…

WmiRevertToPolicyB…              Boolean {}                   {Implemented, Wmi…

WmiSetALSBrightnes…              Boolean {State}              {Implemented, Wmi…

WmiSetALSBrightness                Boolean {Brightness}         {Implemented, Wmi…

I see that there are four methods for the WmiMonitorBrightnessMethods class. The method that looks like it is the best one for my needs is the WmiSetBrightness method. It takes two parameters that I want to look at in more detail. So I adjust my query to focus on it. I use the Item method to specify which method I want to look at. The command is shown here:

PS C:> $wmi.CimClassMethods.Item(‘wmiSetBrightness’)

 

Name                            ReturnType Parameters           Qualifiers

—-                            ———- ———-           ———-

WmiSetBrightness                   Boolean {Brightness, Time… {Implemented, Wmi…

But the parameters information is still truncated. So I add Parameters to the end of my query and arrive at the following:

PS C:> $wmi.CimClassMethods.Item(‘wmiSetBrightness’).parameters

 

Name                               CimType Qualifiers           ReferenceClassName

—-                               ——- ———-           ——————

Brightness                           UInt8 {ID, in}

Timeout                             UInt32 {ID, in}

Cool. Now I know the two parameters that I need to use to call this method. Brightness is expressed as a percentage, and Timeout is expressed in seconds.

I use the Get-WmiObject cmdlet to query the WmiMonitorBrightnessMethods WMI class, and store the returned object in a variable as shown here:

$monitor = Get-WmiObject -ns root/wmi -class wmiMonitorBrightNessMethods

Now, I call the WmiSetBrightNess method and specify that I want the monitor to be at 80% and request that it do so in 10 seconds. Here is the command:

$monitor.WmiSetBrightness(80,10)

Cool! It works. Not sure how I will use this, but it does work.

Well, that is all there is to using the WMI classes to set monitor brightness. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy