Here’s my last word on the subject of PowerShell prompts, for now anyway. The more I worked with prompts, the more I realized the prompt could be a powerful administrative tool. Why not use a prompt to display system information like CPU and drive utilization? Why not system uptime? Why stop there? What about the weather so I can at least know what I’m missing?
First off, here’s the code for the prompt (which is also attached so you don’t have to copy and paste)
Function prompt {
#this function requires the Show-Weather function to be loaded
#$global:zip should be defined in your function for your zip code
#add global variable if it doesn’t already exist
if ($global:LastCheck -eq $Null) {
$global:LastCheck=Get-Date
}
if ($global:weather -eq $Null) {
$global:weather=Show-Weather $global:zip
}
if ($global:cddrive -eq $Null) {
[wmi]$Global:cdrive=gwmi -query “Select * from win32_logicaldisk where deviceid=’c:’”
}
#only refresh weather and disk check once every 15 minutes
$min=(New-TimeSpan $Global:lastCheck).TotalMinutes
if ($min -ge 15) {
$global:weather=Show-Weather $global:zip
[wmi]$Global:cdrive=gwmi -query “Select * from win32_logicaldisk where deviceid=’c:’”
$global:LastCheck=Get-Date
}
$w=([char]0x263c)+” “+$Global:weather.Condition +” “+$Global:weather.Temp
$cpu=(gwmi win32_processor).loadpercentage
$pcount=(Get-Process).Count
$diskinfo=” Free C:”+”{0:N2}” -f (($global:cdrive.freespace/1gb)/($global:cdrive.size/1gb)*100)+”%”
#get uptime
$time=Get-WmiObject -class Win32_OperatingSystem
$t=$time.ConvertToDateTime($time.Lastbootuptime)
[TimeSpan]$uptime=New-TimeSpan $t $(get-date)
$up=”$($uptime.days)d $($uptime.hours)h $($uptime.minutes)m $($uptime.seconds)s”
$text=”CPU:”+$cpu+”% Procs:”+$pcount+$diskinfo+ ” “+([char]0x25b2)+$up +” “+`
(Get-Date -format g)+” “+$w
$myPrompt=[char]0x250c
$myPrompt=$myPrompt+([char]0x2500).ToString()*$text.length
$myPrompt=$myPrompt+[char]0x2510
$myPrompt=$myPrompt+”`n”
$myPrompt=$myPrompt+([char]0x2502)+$text+([char]0x2502)
$myPrompt=$myPrompt+”`n”
$myPrompt=$myPrompt+[char]0x2514
$myPrompt=$myPrompt+([char]0x2500).ToString()*$text.length
$myPrompt=$myPrompt+[char]0x2518
Write-Host $myPrompt -fore Green
Write-Host “PS $(Get-Location) >” -nonewline
return ” “
}
To use this prompt, you should add these global variables to your profile:
$global:zip=your zip code
[wmi]$Global:cdrive=gwmi -query “Select * from win32_logicaldisk where deviceid=’c:’”
$global:LastCheck=Get-Date
The zip code is required if you want to use the weather reporting feature. You’ll also need to add the Show-Weather function to your profile before the Prompt function. Ideally, I would add
$global:weather=Show-Weather $global:zip
To get current weather conditions. If you don’t define the others, the function has code to create them if they don’t exist.
When you put it all together you should get something like this (the graphic is wider than the entry column so save a copy locally if you want to see the entire thing):
The box shows CPU load percentage and the number of processes. These values are calculated every time the prompt refreshes. I’m also showing the percent of free space on drive C:. If you look at the code you’ll see I’m using the -f operator to format the expression. I tried using the percent expression but didn’t like where it left the % symbol so I’ve hacked something together that works for me. This figure is refreshed every 15 minutes.
The system uptime is represented by the Unicode character for the up triangle. It saves some space instead of typing out “Uptime:”.
After the date is the current weather conditions for my zip code. Again, I’m using a Unicode character to indicate weather. The weather will also be updated every 15 minutes. Because I’ve set $weather has a global variable, you can access it any time by typing $weather. The Show-Weather function returns other information but I’m only using the current conditions and temperature. For non-US, you’ll need to go to Weather.yahoo.com and find your location abbreviation. Then you can use that in place of the zip code. The function will work the same.
(Occasionally, the weather condition data doesn’t get populated, but after the next refresh period it usually does. This might also just be me.)
So there you have it. A PowerShell prompt that is more than just a pretty face. There’s almost no limit to what you can do. Want to keep an eye on a critical service or server? Add some WMI code to get the values and display them in your prompt.
Here are some other ideas you might want to try out:
- Number of hours before you go home
- Number of hours before the end of the week
- Read a Message of the day file and display something inspiring, pithy or silly
- OS and Service Pack
- Network performance counters
- Number of new messages in your inbox
- Use a reminder file: Enter each reminder on its own line. Read the file and cycle through the reminders in your prompt.
- Get the size of your SMTP queue in Exchange 2003
What will you do with your prompt? Share your adventures in the PowerShell forum at ScriptingAnswers.com.
1 comment on “One Prompt to Rule Them All”
Comments are closed.