Do you need some prompting?

During Don’s TechEd session that was simulcast on the web, I was furiously answering questions for the online crowd. One of the questions was about Don’s PowerShell prompt.  Every time he pressed Enter, a command counter was incremented and displayed like this:

[137] PSH C:\temp >

I don’t exactly what Don used, but here’s a prompt function that I came up with to achieve this effect:

Function Prompt {
#[137] PSH C:\temp >
# You need to define $global:i=0
# in your profile before you run this function
# You can reset $i to 0 at any time if you want
# to “reset” the prompt counter
$global:i++
”[“+$i+”] PSH ” +  $(Get-Location) + ” > “
}

In your profile, which is where you would add this function, you’ll also need to define a global counter variable.

$global:i=0

The prompt function will increment this counter each time you press Enter. If you want to start again at 0, simply set $i back to 0. Short and simple.

But this got me thinking…what else can I do with my PowerShell prompt? My prompt includes the current date and location displayed in yellow:

Wed 6/6/2007 12:29:28 PM PS C:\Documents and Settings\jhicks >

But what about having a prompt that displays CPU utilization and the number of running processes:

[CPU:7% Procs:66] PS C:\temp >

What about a prompt that displays the current user:

PS [SAPIEN\JHICKS 3:03 PM] C:\temp >

One of my favorites that I came up with, at least for a short term, is to have the current location spoken:

function Prompt {
#Install SAPIEN Community extensions and use Write-Speech to
#speak the current directory
Get-Location | Write-Speech
$myPrompt=”PSH ” +  $(Get-Location) + ” > “
Write-Host ($myPrompt) -nonewline
return ” “
}

I’ve attached a text file with all the prompt functions that I’ve mentioned here plus a few more.  Once you look at the samples you’ll see how easy it is to mix and match information.

Enjoy.

Technorati Tags: ,

del.icio.us Tags: ,