Prompts in PowerShell
Table of contents for Intro to PowerShell
- Getting PowerShell
- Basic PowerShell Concepts
- Commonly Used Cmdlets
- PowerShell Scripts and Profiles
- Using PowerShell types with commands and variables
- Error handling in PowerShell scripts
- Calling static methods
- $NestedPromptLevel
- Prompts in PowerShell
PowerShell provides a very flexible mechanism for specifying what to display for the prompt, particularly since you have the entirety of the .NET Framework at your disposal. PowerShell displays the prompt using the Prompt function. By default it is defined like this:
'PS ' + $(Get-Location) + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '
This function returns a string that contains the following elements:
- PS (to indicate that you are in a PowerShell window and not a Command Prompt window).
- The current location (e.g. C:\Users).
- Whether you are in a nested prompt level or not (see this article for more information on nested prompt levels).
As can be seen by this simple function, we are not limited to displaying a simple string since we have the full power of the .NET Framework at our disposal. Here are some examples of what can be done:
- Display:
- Current location - Get-Location OR $PWD.Path
- Location stack depth - (Get-Location -Stack).Count
- Nested prompt level depth - $NestedPromptLevel
- Command history entry number - (Get-History -Count 1).Id
- Host name information - $Env:COMPUTERNAME
- User identity information - $Env:USERNAME
- Change the color of the prompt and/or the window.
- Display the current location on a different line, maybe only doing so when it exceeds a certain length.
- Set the text on the title bar.
- Change the prompt based on whether it is in admin mode or not.
- Many other things.
Doing a search online for PowerShell prompt will list many web sites where folks are sharing their prompt functions. Here are a few that I found that helped me in assembling my prompt function:
- PowerShell For Fun - Perfect Prompt for Windows PowerShell
- ::::::::: POWERSHELL ::::::::: - "Prompt" function
- Iain’s World - Custom PowerShell Prompt
- commonality - My PowerShell Prompt
- Dave Mohundro - Customize your command prompt!
- The Endeavor - Customizing the PowerShell command prompt
- The Endeavor - Customizing the PowerShell command prompt II
- Stack overflow - Customizing the PowerShell Prompt - Equivalent to CMD’s $M$P$_$+$G?
- Huddled Masses - PowerShell Power User Tips: A Better Prompt
- WinPowerShell - PowerShell Prompt Part 2
My PowerShell prompt is based on the PowerShell Community Extensions, so I’ll share it when I write about the prompt functionality in PSCX.




Leave a Reply