Customize Your PowerShell Prompt

I've always been happy with the default PowerShell prompt, but one of the most-asked questions in class is "can it be customized?" The answer: Yup.

Most of PowerShell's internal configuration settings are actually stored in variables, so running Dir Variable: is a good way to start the customization-discovery process. There, you'll see $prompt, which is the... er, oops. Not there. Well, the other place you'll find customization points is in the Function: drive, and sure enough, there's a built-in "prompt" function. Essentially, the shell calls that function each time it needs to display a prompt, and whatever the function returns is the prompt. The default function looks like this:

$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS
 ' + $(Get-Location) + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '

See how this uses the literal string "PS ", along with the results from Get-Location, to construct the familiar "PS C:>" prompt? Also, it checks the shell's $nestedpromptlevel variable and adds a >> to the prompt if you're in a nested prompt.

All you have to do is define a new prompt function, which will overwrite the existing one, to define a custom prompt. Your work will be undone each time you start a new shell window, so there's no fear of permanently breaking everything; once you have a prompt you like, you can drop the new function into a profile script to have it load each time you open the shell. There's a great article on prompts and profiles here, with some fun variations to try.

What's in your prompt?

Please or Register to post comments.

What's PowerShell with a Purpose Blog?

Don Jones demystifies Windows PowerShell.

Blog Archive

Sponsored Introduction Continue on to (or wait seconds) ×