A Prompt Response

Yesterday, fellow blogger and Powershell guru, Jeff Hicks posted an article about how to do some neat things with your Powershell prompt.  (You can find his article here.)

To add to his entry, I would like to share how I create my Powershell prompt.  I actually have three functions in my profile to get the work done.  They are rather small functions, but by splitting it up, it makes things a little easier to follow.

The first part of my prompt script actually doesn’t change the prompt itself.  However, it does run every time I submit a command so that the title bar is updated.  Since I sometimes forget to reboot my computer on a regular basis (yes, I do run updates!), it is helpful to know how long it has been since the computer was rebooted.  This first function calculates and returns a string to tell me that information.

function get-uptime
{
$lastBootTime = [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime)
$duration = New-TimeSpan -Start $lastBootTime -End (get-date)
return "{0} days {1} hours {2} minutes {3} seconds" -f $($duration.Days), $($duration.Hours), $($duration.Minutes), $($duration.Seconds)
}

This command simply uses the LastBootUpTime property from the WMI class Win32_OperatingSystem and returns a human-readable string.

The second function accesses the Win32_Battery class to get the EstimatedChargeRemaining property.  Based on the charge percentage left, the function returns a color to be used in the prompt function.  I have used four colors to indicate a full charge, 50% charge, 25% charge, and less than 25% charge.

function Get-LaptopBatteryStatus
{
$charge = (Get-WmiObject -Class Win32_Battery).EstimatedChargeRemaining
if ($charge -ge 100)
{ return "White" }
if ($charge -ge 50)
{ return "Green" }
if ($charge -ge 25)
{ return "yellow" }
return "red"
}

Now to bring it all together.  The final function actually creates the prompt and runs these other functions to make the magic happen.  Every time I enter a command, the prompt function updates the title bar and changes the color of the prompt to indicate how much battery I have left.  It also shows me the username and computer name that I am using, which is helpful if I am logged in as multiple users (with different admin levels) to know which console to activate.  The battery indicator is not meant to be a precise
indicator, but it does alert me in plenty of time that I need to get to an outlet for some more juice.

function prompt
{
$host.ui.rawui.WindowTitle = "{0} - {1} - {2} - Uptime: {3}" -f $ENV:username, $env:COMPUTERNAME, $(get-date -Format MM/dd/yyyy), $(get-uptime)
Write-Host
Write-Host "PS $(get-location)> " -NoNewline -ForegroundColor $(Get-LaptopBatteryStatus)
return " "
}

PowershellPromptImage
The nice thing about Powershell is that you can incorporate as many changes like this as you feel necessary to get your work done.  When you get a chance, head over to Jeff Hicks’ blog to get some more ideas on how Powershell can help you.

Advertisements
This entry was posted in Beginner, Scripting and tagged , , , , , . Bookmark the permalink.