Displaying Battery Charge in your prompt

by Jul 6, 2009

If you use a notebook and are on the road often, you may want a way to check the battery status so you can shut down your stuff in time. Here is a custom prompt function which displays the remaining charge in your prompt and also displays the current path in your consoles title bar:

function prompt {
$charge = (Get-WmiObject win32_battery |
Measure-Object -property EstimatedChargeRemaining -average).Average

$prompt = "PS [$charge%]> "

if ($charge -lt 30) {
Write-Host $prompt -foregroundcolor Red -noNewLine
} elseif ($ladung -lt 60) {
Write-Host $prompt -foregroundcolor Yellow -noNewLine
} else {
Write-Host $prompt -foregroundcolor Green -noNewLine
}

$Host.UI.RawUI.WindowTitle = (Get-Location)
" "
}

Note how your prompt will change color from green over yellow to red, depending on the remaining battery charge.