Show Battery Status as Prompt

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

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'
' '
}

Posted Dec 09 2008, 08:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.