Show Battery Status as Prompt

by Dec 9, 2008

The PowerShell console prompt can be easily changed by simply changing the prompt function to change the prompt text. If you are working with a notebook, you may want to get a warning when battery power goes low. Use WMI to query the battery status and then use this information to format the prompt:

function prompt {
$charge = Get-WMIObject Win32_Battery -property EstimatedChargeRemaining
switch ($charge.EstimatedChargeRemaining) {
{ $_ -lt 25 } { $color = 'red'break }
{ $_ -lt 50 } { $color = 'red'break }
default { $color = 'white' }
}
$text = 'PS {0} ({1}%)> ' -f (Get-Location), $charge.EstimatedChargeRemaining
Write-Host $text -nonewline -foreground $color -background 'black'
' '
}