{"id":148,"date":"2007-06-13T11:00:00","date_gmt":"2007-06-13T19:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/06\/13\/one-prompt-to-rule-them-all\/"},"modified":"2007-06-13T11:00:00","modified_gmt":"2007-06-13T19:00:00","slug":"one-prompt-to-rule-them-all","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/06\/13\/one-prompt-to-rule-them-all\/","title":{"rendered":"One Prompt to Rule Them All"},"content":{"rendered":"<p>Here&#8217;s my last word on the subject of PowerShell prompts, for now anyway.&nbsp; 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?&nbsp; Why stop there?&nbsp; What about the weather so I can at least know what I&#8217;m missing?<\/p>\n<p>First off, here&#8217;s the code for the prompt (which is also attached so you don&#8217;t have to copy and paste)<\/p>\n<p><font face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Function prompt {<br \/>#this function requires the Show-Weather function to be loaded<br \/>#$global:zip should be defined in your function for your zip code <\/font><\/p>\n<p><font face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">#add global variable if it doesn&#8217;t already exist<br \/>if ($global:LastCheck -eq $Null) {<br \/>&nbsp;&nbsp;&nbsp; $global:LastCheck=Get-Date<br \/>}<br \/>if ($global:weather -eq $Null) {<br \/>&nbsp;&nbsp;&nbsp; $global:weather=Show-Weather $global:zip<br \/>}<br \/>if ($global:cddrive -eq $Null) {<br \/>&nbsp;&nbsp;&nbsp; [wmi]$Global:cdrive=gwmi -query &#8220;Select * from win32_logicaldisk where deviceid=&#8217;c:&#8217;&#8221;<br \/>} <\/font><\/p>\n<p><font face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">#only refresh weather and disk check once every 15 minutes<br \/>$min=(New-TimeSpan $Global:lastCheck).TotalMinutes<br \/>if ($min -ge 15) {<br \/>$global:weather=Show-Weather $global:zip<br \/>[wmi]$Global:cdrive=gwmi -query &#8220;Select * from win32_logicaldisk where deviceid=&#8217;c:&#8217;&#8221;<br \/>$global:LastCheck=Get-Date<br \/>} <\/font><\/p>\n<p><font face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$w=([char]0x263c)+&#8221; &#8220;+$Global:weather.Condition +&#8221; &#8220;+$Global:weather.Temp<br \/>$cpu=(gwmi win32_processor).loadpercentage<br \/>$pcount=(Get-Process).Count<br \/>$diskinfo=&#8221; Free C:&#8221;+&#8221;{0:N2}&#8221; -f (($global:cdrive.freespace\/1gb)\/($global:cdrive.size\/1gb)*100)+&#8221;%&#8221;<br \/>#get uptime<br \/>$time=Get-WmiObject -class Win32_OperatingSystem<br \/>$t=$time.ConvertToDateTime($time.Lastbootuptime)<br \/>[TimeSpan]$uptime=New-TimeSpan $t $(get-date)<br \/>$up=&#8221;$($uptime.days)d $($uptime.hours)h $($uptime.minutes)m $($uptime.seconds)s&#8221; <\/font><\/p>\n<p><font face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$text=&#8221;CPU:&#8221;+$cpu+&#8221;% Procs:&#8221;+$pcount+$diskinfo+ &#8221; &#8220;+([char]0x25b2)+$up +&#8221; &#8220;+`<br \/>(Get-Date -format g)+&#8221; &#8220;+$w <\/font><\/p>\n<p><font face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$myPrompt=[char]0x250c<br \/>$myPrompt=$myPrompt+([char]0x2500).ToString()*$text.length<br \/>$myPrompt=$myPrompt+[char]0x2510<br \/>$myPrompt=$myPrompt+&#8221;`n&#8221;<br \/>$myPrompt=$myPrompt+([char]0x2502)+$text+([char]0x2502)<br \/>$myPrompt=$myPrompt+&#8221;`n&#8221;<br \/>$myPrompt=$myPrompt+[char]0x2514<br \/>$myPrompt=$myPrompt+([char]0x2500).ToString()*$text.length<br \/>$myPrompt=$myPrompt+[char]0x2518<br \/>Write-Host $myPrompt -fore Green <br \/>Write-Host &#8220;PS $(Get-Location) &gt;&#8221; -nonewline<br \/>return &#8221; &#8220;<br \/>}<\/font> <\/p>\n<p>To use this prompt, you should add these global variables to your profile:<\/p>\n<p><font face=\"Lucida Con\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$global:zip=your zip code<br \/>[wmi]$Global:cdrive=gwmi -query &#8220;Select * from win32_logicaldisk where deviceid=&#8217;c:&#8217;&#8221;<br \/>$global:LastCheck=Get-Date<\/font><\/p>\n<p>The zip code is required if you want to use the weather reporting feature. You&#8217;ll also need&nbsp;to add the&nbsp;<a href=\"\/storage\/show-weather.ps1\">Show-Weather <\/a>function to your profile before the&nbsp;Prompt function. Ideally, I would add<\/p>\n<p><font face=\"Lucida Console\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">$global:weather=Show-Weather $global:zip<br \/><\/font><\/p>\n<p>To get current weather conditions.&nbsp;If you don&#8217;t define the others, the function has code to create them if they don&#8217;t exist.<\/p>\n<p>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):<\/p>\n<p><span class=\"full-image-float-left\"><img decoding=\"async\" src=\"\/storage\/jdhboxprompt.jpg\" alt=\"jdhboxprompt.jpg\" style=\"width: 590px; height: 50px;\" \/><\/span><\/p>\n<p>The box shows CPU load percentage and the number of processes. These values are calculated every time the prompt refreshes. I&#8217;m also showing the percent of free space on drive C:&#46; If you look at the code you&#8217;ll see I&#8217;m using the -f operator to format the expression. I tried using the percent expression but didn&#8217;t like where it left the % symbol so I&#8217;ve hacked something together that works for me.&nbsp; This figure is refreshed every 15 minutes.<\/p>\n<p>The system uptime is represented by the Unicode character for the up triangle. It saves some space instead of typing out &#8220;Uptime:&#8221;.<\/p>\n<p>After the date is the current weather conditions for my zip code. Again, I&#8217;m using a Unicode character to indicate weather. The weather will also be updated every 15 minutes. Because I&#8217;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&#8217;m only using the current conditions and temperature.&nbsp; For non-US, you&#8217;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. <\/p>\n<p>(Occasionally, the weather condition data doesn&#8217;t get populated, but after the next refresh period it usually does. This might also just be me.)<\/p>\n<p>So there you have it.&nbsp; A PowerShell prompt that is more than just a pretty face. There&#8217;s almost no limit to what you can do.&nbsp; 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.<\/p>\n<p>Here are some other ideas you might want to try out:<\/p>\n<ul>\n<li>Number of hours before you go home<\/li>\n<li>Number of hours before the end of the week<\/li>\n<li>Read a Message of the day file and display something inspiring, pithy or silly<\/li>\n<li>OS and Service Pack<\/li>\n<li>Network performance counters<\/li>\n<li>Number of new messages in your inbox<\/li>\n<li>Use a reminder file: Enter each reminder on its own line. Read the file and cycle through the reminders in your prompt.<\/li>\n<li>Get the size of your SMTP queue in Exchange 2003<\/li>\n<\/ul>\n<p>What will you do with <strong><em>your<\/em><\/strong> prompt? Share your adventures in the PowerShell forum at ScriptingAnswers.com. <\/p>\n<div style=\"margin: 0px; padding: 0px; display: inline;\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:3ae869df-d434-40a1-99a8-800cd06d38d6\" class=\"wlWriterSmartContent\"><span class=\"sizeLess20\">Technorati Tags: <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Powershell\"><span class=\"sizeLess20\">Powershell<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Prompt\"><span class=\"sizeLess20\">Prompt<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/function\"><span class=\"sizeLess20\">function<\/span><\/a><\/div>\n<p><\/p>\n<div style=\"margin: 0px; padding: 0px; display: inline;\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:4236ecd6-0dfc-452c-9686-190787e01f12\" class=\"wlWriterSmartContent\"><span class=\"sizeLess20\">del.icio.us Tags: <\/span><a rel=\"tag\" href=\"http:\/\/del.icio.us\/popular\/Powershell\"><span class=\"sizeLess20\">Powershell<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/del.icio.us\/popular\/prompt\"><span class=\"sizeLess20\">prompt<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/del.icio.us\/popular\/function\"><span class=\"sizeLess20\">function<\/span><\/a><\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;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&#8217;m missing?<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}