Home > Powershell > Just a bit of PoSh – show your battery level at the prompt

Just a bit of PoSh – show your battery level at the prompt

I’ve been working on a new, more condensed version of the OpsMgr PoSh library – one of the coolest features I think is the ‘location awareness’ which automatically switches your management server and such based on where you’re actually connected from.

Anyway, to do that I was embedding things in function prompt {}. In case you aren’t aware, if you define the prompt function, whatever is in there will run everytime you return to the prompt in the console. You can use this to your advantage for many things, such as this nifty little battery gauge that lets you know how much juice is left. It also has a configurable global variable, BatteryDisplayAtPercent, which you can set so it will hide until there is that much charge or less remaining.

First a screenshot, then the script:

battery-prompt

# ==============================================================================================
#
# Microsoft PowerShell Source File — Created with SAPIEN Technologies PrimalScript 2007
#
# NAME: Battery-Prompt.ps1
#
# AUTHOR: Jeremy D. Pavleck , Pavleck.NET
# DATE  : 12/14/2008
#
# COMMENT: Lists the percentage of battery remaining above your prompt inside the console.
#    Difficulty level: OVER 9000!!!!!!!!!!!!!!!!
#
# ==============================================================================================
$GLOBAL:BatteryDisplayAtPercent = 101 # When you should start displaying status
# Anything over 100 means to show it all
Function GLOBAL:Get-BattLevel($minLevel) {
$charge = (Get-WmiObject -Class Win32_Battery).EstimatedChargeRemaining
If(!$charge) {break} # Not on a laptop, or no battery, so exit
Switch($charge) {
{($charge -ge 101) -and ($minLevel -ge 101)} {Write-Host "Batt: CHARGING" -ForeGroundColor Green}
{($charge -ge 80) -and ($charge -le 100) -and ($charge -le $minLevel)} {Write-Host "Batt: $charge%" -ForeGroundColor Green}
{($charge -ge 40) -and ($charge -le 79) -and ($charge -le $minLevel)} {Write-Host "Batt: $charge%" -ForeGroundColor Yellow}
{($charge -ge 16) -and ($charge -le 39) -and ($charge -le $minLevel)} {Write-Host "Batt: $charge%" -ForeGroundColor Magenta}
{($charge -ge 1) -and ($charge -le 15) -and ($charge -le $minLevel)} {Write-Host "Batt: $charge%" -ForeGroundColor Red}
default {break}
}
}

function prompt() {
Get-BattLevel $GLOBAL:BatteryDisplayAtPercent
}

Categories: Powershell Tags: , ,
  1. December 27th, 2008 at 04:34 | #1

    This has been widely known and it is a useful function I usually keep on my laptop.
    The first example I know of dates back to 2006 tho:
    http://pcmusings.spaces.live.com/Blog/cns!42B71883C19FDDAE!294.entry

  2. Jeremy D. Pavleck
    December 29th, 2008 at 13:09 | #2

    Oh, I know I wasn’t the first, but I wanted to throw something up there – the Mobile OpsMgr library and AlertWrangler are taking their sweet time in coming about, so I’m running a tad dry on finished things – probably because everything is getting to be a large project.

  1. No trackbacks yet.