Windows Tip Sheet

PowerShell Title Prompts

Replace your title bar prompts to something more useful. Yes, behold the power of PowerShell.

In the past I've talked about different ways to customize your PowerShell prompt. (Examples are here and here.) Well, here's a new one:

Function Prompt {
Switch ((get-date).dayofweek.toString())
{
  Sunday {$dow="Sun";break}
  Monday {$dow="Mon";break}
  Tuesday {$dow="Tue";break}
  Wednesday {$dow="Wed";break}
  Thursday {$dow="Thu";break}
  Friday {$dow="Fri";break}
  Saturday {$dow="Sat";break}
}

$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"

$NewTitle="PowerShell [" +`
  $dow+ " " + (get-date).ToShortDateString() + `
" " + (get-date).ToLongTimeString()+ " " + `
$up + "] " + (get-location).toString()

$host.ui.rawui.set_WindowTitle($NewTitle)

#define "real" prompt
$myPrompt=" PS >"
  Write-Host ($myPrompt) -nonewline -foregroundcolor Yellow
  return " "
}

(When you copy this, make sure that all the quotes are straight quotes and the right angle bracket is a right angle bracket -- formatting sometimes gets a little skewed when this is published to the Web.)

This function will replace your current prompt. When you use it, the prompt you see in the Windows will be PS >. But the Title bar is where the magic happens. Everytime you press Enter, the Title bar will change. As written it will show something like this:

PowerShell [Mon 7/2/2007 8:43:10 PM 5d 4h 41m 32s] C:\

The values after the date and time is the uptime for the current system. The last part of the title is your current path. I always needed more room at my PowerShell prompt so moving the current directory to the Title freed up a lot of space. You can always use the pwd alias to display the current directory.

You can try this out without making any permanent changes. Simply copy the above and paste at a PowerShell prompt. This will change your current prompt, but only until you close PowerShell. The next time you open PowerShell you'll have your original prompt. If you want to keep this, or a modified version, simply paste the function at the end of your PowerShell profile.

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at [email protected]; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

If you prefer, move the location back to the "real" prompt. But there are many other possibilities for the title area. You might want to add the current temperature, the computer name, the name of the logged on user, amount of free hard drive space, CPU utilization, memory usage ... the opportunities are practically endless. PowerShell can give you a wealth of information. You just have to be creative in what to do with it.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular