More Battery Reporting Fun with PowerShell

Posted on December 5, 2014 by Jeff Hicks in PowerShell with 0 Comments

In an earlier article I guided you through the process of creating a PowerShell based tool to keep track of battery power. Even if you didn’t need the end result, the process I followed and techniques can be helpful in your own projects. In this article I want to continue exploring what I can do with my previous work. I’m always looking to see how far I can push my PowerShell projects. Sometimes PowerShell, or Windows, pushes back but even that can be educational.

With my battery check tool, one limiting factor is that I have to remember to periodically run the function in my PowerShell session. But then there’s that whole typing-thing. Oh, the agony. To alleviate my pain and suffering, I first need something that will give me a more visual clue about battery life.

Creating a PowerShell Battery Report Popup Dialog

I decided I wanted an old-fashioned popup box. Being an old-school VBScript guy I always liked the Popup() method from the Wscript.Shell object. This is even easier to use in PowerShell.

powershell battery report

Using to create a popup dialog. (Image: Jeff Hicks)

The Popup() method has additional parameters to control the title, the button selection and icons. What I especially like about this method is that unlike the VBScript MessageBox, I can set a timeout value so that if I don’t click on a button, the popup will still automatically dismiss after a specified number of seconds.

Setting a time out value using $shell.Popup. (Image: Jeff Hicks)

The second parameter, 10, is the number of seconds to display the popup. If you set it to -1 the popup will remain until the user clicks a button. The last parameter is a combination of buttons and icons. You can learn more about these values by reading the official MSDN documentation for the popup method.

For my needs, I only need to present a popup if the battery level reaches a certain level and is not connected to power. Here’s the brief monitoring script I came up with that I call Watch-Battery.ps1.

The script checks the Win32_Battery class and compares the EstimatedChargeRemaining property to the parameterized Limit. If the value is less than or equal than the limit, then the script creates and displays the popup. The TimeOut parameter controls how long the popup is displayed in seconds.


I use a here string to define the message, inserting values from variables. When using a here string in a script file, it needs to be left justified. Technically not all of it does but I find it just as easy to left justify the entire thing.

Here’s an example of the script in action. At the prompt I can type:

And get this:

A sample battery alert created using PowerShell. (Image: Jeff Hicks)

Of course I still have to manually type the command. I first tried creating a PowerShell scheduled job.

While the job technically ran, I never saw the popup on my screen. PowerShell scheduled jobs run in separate PowerShell instances that are not interactive when launched by the Task Scheduler.


Although I suppose instead of displaying the popup I could have the script email me or send me a text message on my phone. But the interactive popup appears to be a non-starter with scheduled jobs. I did try using a Scheduled Task which worked slightly better.

I got the popup, but I end up with a flashing PowerShell window. So what to do? I’ll cover that in a separate article.

In the meantime, look at your PowerShell tools and see where else you can take them. It can make for an exciting journey.


Tagged with ,