Laptops are cool for some scripting on the go. But sometimes you need to check on your juices. You don’t want to end your PowerShell session mid-command! Enter the commandline battery indicator.

How to check your battery level?

You can use Get-CimInstance to check on your win32_battery. This contains lots of information about your battery or batteries if you have multiple batteries in your system.

$ BatteryLevel = ( Get-CimInstance - ClassName Win32_battery).EstimatedChargeRemaining[ 0 ] / 10



We can select our battery by selecting the first item using [0]. Then we divide by 10 to convert our percentage in EstimatedChargeRemaining to a number between 1 and 10. That’s easier for later use.

Create a nice battery level indicator

We will use some Unicode characters to build our battery indicator. For each number below or equal to our EstimatedChargeRemaining / 10, we display a coloured dark shaded block. The other block will be light shaded and have our default prompt colour.

0 .. 10 | ForEach-Object { if ( $ _ -le $ BatteryLevel ) { write-host "▓" - NoNewline - ForegroundColor "DarkCyan"

}

else {

write-host "░" - NoNewline }



Result

As you can see below I integrated this bar in my PowerShell prompt. No more leaving my toned-down console and using my mouse to check on my battery!

Oh-oh that bar is dangerously low! Quick typing now…

Add the function to your Ultimate PowerShell prompt

function Get-BatteryLevel { $ BatteryLevel = ( Get-CimInstance - ClassName Win32_battery).EstimatedChargeRemaining[ 0 ] / 10

0 .. 10 | ForEach-Object { if ( $ _ -le $ BatteryLevel ) { write-host "▓" - NoNewline - ForegroundColor "DarkCyan"

}

else {

write-host "░" - NoNewline } } }

Searching for a guide on how to customize your own prompt? Check out Matthew Hodgkins blog. He has some next-level stuff.

Some improvements!

Many kudos to da_chicken for some nice improvements!

1. Add the -Property parameter to Get-CimInstance

According to documentation this will reduce the size of the object returned.

Specifies a set of instance properties to retrieve. Use this parameter when you need to reduce the size of the object returned, either in memory or over the network. The object returned always has key properties populated, irrespective of the set of properties listed by the Property parameter. Other properties of the class are present but they are not populated.

Nice! Even more memory available.

2. Use $BatteryLevel, $null = to fetch just the first value

As mentioned by da_chicken we don’t want error’s puked each time we enter a command. Some people only have one battery in their laptops… (How do you survive???)

This will cause an error because we do not have an array if only battery is installed. Using [0] is a bad idea.

We can prevent “Indexing a null array” by using following line:

$ BatteryLevel , $null = ( Get-CimInstance - ClassName Win32_battery - Property EstimatedChargeRemaining).EstimatedChargeRemaining

3. Use string multiplication instead of a pipeline, loop, and if statements

Maybe a for loop with a nested if statement was a bit overkill.

Unfortunately the extra CPU cycles have killed my batte One unfortunate Reddit user killed his battery mid sentence… :-s

write-host ( "▓" * $ BatteryLevel ) - NoNewline - ForegroundColor "DarkCyan" write-host ( "░" * ( 10 - $ BatteryLevel )) - NoNewline



The solution above is much neater! I was wondering why I had some frames dropping in a little bit later.

Improved version