Scott Hanselman

A better PROMPT for CMD.EXE or Cool Prompt Environment Variables and a nice transparent multi-prompt

July 14, 2006 Comment on this post [10] Posted in PowerShell | ASP.NET | XML | Tools
Sponsored By

A number a people have commented on my simple, but customized command prompt. Actually I have a few, but for cmd.exe, I do this. Right-click on My Computer|Properties. Then from the Advanced Tab, click Environment Variables, then add a new User Variable called PROMPT and set it to $p$_$+$g. This came originally from Craig Andera, who got it from Shawn Van Ness. Sahil Malik also has some great command line tricks. Junfeng points out the little known ntcmds.chm.

A screenshot of the System Properties Dialogs

Here's the breakdown:

  • $P = Current Directory's Path
  • $_ = Carriage Return
  • $+ = A plus sign for each level in the PUSHD/POPD stack.

Here's some other interesting prompts. Paste these into any CMD.EXE window:

set prompt=[%computername%] $d$s$t$_$p$_$_$+$g

yields this. Note the use of an environment variables within a prompt. Here's an extensive list of environment variables.

[SCOTTPC] Thu 07/13/2006 22:19:20.40
C:\Documents and Settings\Scott

Another nice one:

set=prompt=$m$_$p$g

yields this when on a UNC Mapped Drive:

\\FREDPC\C$
Z:\Documents and Settings\Fred

There's all sorts of crap that PROMPT /? gives you:

  $A   & (Ampersand)
  $B   | (pipe)
  $C   ( (Left parenthesis)
  $D   Current date
  $E   Escape code (ASCII code 27)
  $F   ) (Right parenthesis)
  $G   > (greater-than sign)
  $H   Backspace (erases previous character)
  $L   < (less-than sign)
  $N   Current drive
  $P   Current drive and path
  $Q   = (equal sign)
  $S     (space)
  $T   Current time
  $V   Windows XP version number
  $_   Carriage return and linefeed
  $$   $ (dollar sign)
  $+   zero or more plus sign (+) characters depending upon the
       depth of the PUSHD directory stack, one character for each
       level pushed.
  $M   Displays the remote name associated with the current drive
       letter or the empty string if current drive is not a network
       drive.

Here's my command prompt. It's transparent because I use the awesome Console 2.00 Beta, build 122 hosted at SourceForge. I blogged about this last November.

Note the multiple tabs. I've got PowerShell, CMD.EXE and two VisualStudio Windows, each in their own tab. I could add a tab for Cygwin, but really, with PowerShell, who needs ls -alogF?

Scottconsole1

Console uses a XML settings file (console.xml) that takes a while to understand. Here's a snippet of my settings. I used short filenames in the VS.NET stuff for simplicity and avoidance of quoted quotes. Remember, DIR /X will give you short filenames for things like this.

...Snip...this is a PARTIAL snippet for illustrative purposes...

<tabs>
  <tab title="Console">
   <console shell="" init_dir=""/>
  </tab>
  <tab title="PowerShell">
   <console shell="c:\program files\windows powershell\v1.0\powershell.exe" init_dir=""/>
  </tab>
  <tab title="VS.NET 2003">
   <console shell="cmd /k C:\PROGRA~1\MICROS~2.NET\Common7\Tools\vsvars32.bat" init_dir=""/>
  </tab>
  <tab title="VS.NET 2005">
   <console shell="cmd /k C:\PROGRA~1\MID05A~1\VC\vcvarsall.bat" init_dir=""/>
  </tab>
 </tabs>

My font is Consolas, Size 15, Kermit Green (0x00FF00). Enjoy.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
July 14, 2006 11:44
Tell me again why transparent ANYTHING is more usable?

It's like staring into a magic eye painting. I've gone crosseyed.

At least with Aero Glass, the non-functional areas of the window are semi-transparent. The main work area of the window is still opaque and easy to read.
July 14, 2006 16:31
Your tip on the PROMPT environment variable to customize the prompt in cmd was great. I've set up a similar scenario in PowerShell be defining a prompt function (thanks, Lee Holmes) that looks like this:

function prompt { "PS " + (get-location).Path + [System.Environment]::NewLine + ">" }

I've placed it in my PowerShell profile folder, but I haven't yet discovered a means of adding the pushd/popd stack. Have you done something similar with your PowerShell prompt?
July 14, 2006 18:41
hi, scott i try activate tabs on my console application and nothing happens, you customize your application or my settings are wrong?
July 14, 2006 19:39
Just a thought concerning the tabbed console app that gave me an idea of my own...at work I have close to a dozen console shortcuts that allow me to do different tasks...in essence think Visual Studio command window only on steroids...

My thought is this: I wonder how well this app would work to allow you to manage these various environments...

...Either way it is not unusual for me to have 4-6 consoles open at once, and it's almost remeniscent of a huge game of hunt the wumpus especially when each one has different environment settings....
July 14, 2006 19:42
Never mind on my previous question - I just discovered get-location with -stack parameter. Calling (get-location -stack).count will return the pushd/popd stack depth.
July 15, 2006 18:49
Cool, thanks Dave. I added this to my profile to display the stack depth. It now behaves much like $+ does in CMD.EXE's prompt.

$depth = ""

for($i = 0; $i -lt (get-location -stack).count; $i++)
{ $depth += "+" }
July 15, 2006 18:58
Regarding Console, I noticed my colorized PowerShell prompt works fine if I accept Console's default color scheme.

However, if I go into Edit | Settings | Appearance and select a custom color for my font, that color "takes over" and everything appears in that color. Colors specified in PowerShell (e.g. write-host -foregroundcolor red "Hello World") are ignored.

Anyone know how to get around this?
July 15, 2006 23:35
Arthur: Console lets you set up different shells and environment for each type of tab. You can see as much from Scott's screenshot as well. Personally, I have one tab type for VS 2005 environment + Powershell, one for VS 2002 + Powershell (I build Ankh using .NET 1.0) and one for VS 2005 + cmd.exe. At work I have some other variations as well.
July 17, 2006 23:24
Oh man, Scott you're ruining my life!

The possibilities for Console 2 (I must look at the eConsole original too) had my brain spinning. I was thinking the same as Arthur, and I love what Arild does.

The idea of an IDE wrapper for console sessions is super-cool. I can also see it as a way to build a friendlier way for newbies to learn how to deal with this world, something that has to be dealt with as part of learning to do native system development (i.e., in conjunction with VC++ 2005 Express Edition).

Not like I haven't created enough incomplete projects for myself or anything like that, but this is definitely on my list for supporting new developers once I've completed on my promise to document console-sessions for beginners.

- Dennis

PS: Being groggy, I clicked "Kick It" and now have no idea what else I am getting myself into. I figured it was new geek-speak for leaving comments. Sigh.

PPS: I'm at a wedding on July 22, so I won't make the Portland CodeCamp. Maybe I can wear my startling Seattle CodeCamp T-shirt under my suit? Sort of a secret sympathy move?

July 19, 2006 11:22
A great set of tips that have prompted me to use:

[%ComputerName%\%Username%] $t$_$p$_$_$+$g

Which helps me a lot as I 'Run As' different accounts a lot according to required privileges at the time.
Now I should start looking into how to make PowerShell do this as well (unless someone already has this figured and can post it).

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.