Use PowerShell to Report and Set Monitor Brightness

Use PowerShell to Report and Set Monitor Brightness

  • Comments 6
  • Likes

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      : DISPLAY\LEN40B2\4&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 DISPLAY\LEN40B2\4&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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi, im new to powershell. When I run this command

    Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness

    It just says

    Get-Ciminstance : Not supported

    At line:1 char:1

    Any ideas?

  • @Retro according to MSDN msdn.microsoft.com/.../aa394536(v=vs.85).aspx it should work on Vista and Above. I ran this on my Windows 8 laptop (64 bit) and it works as indicated above.

  • @Ed @retro

    It only works on systems that can dynamically set the brightness.  This usually only laptops although some All-In-Ones may have this capability..

    On my laptop this works.  On my desktop it says "Not supported" which is correct because I cannot set the brightness from the OS.  I can only do that on the monitor.  Very few full monitors allow adjustment from the OS and only if you install the monitor driver.

  • @JRV Great catch! It has been so long since I used an actual desktop, that I forget about them. Thank for testing this out!

  • @Ed

    Coincidence but I ran through all of that only about two days before you posted it.  I had to do a little thinking and research to see why and how it worked.

    If anyone has an HP All-In-One I suspect it will work on those.  I believe they are mostly laptop technology on the desktop.

    Newer monitors are also supporting the 'auto-daylight-detect' feature that is in most laptops and adding it.  Newer monitors can also run USB over the video path.  Tis makes them available to self-install the driver and to support thumb drives, mice and keyboards.

    it's an everything-everywhere world.

  • @Ed @Jrv Thanks. That explains it. It was confusing the heck out of me :)