Whenever I post a screen shot of my PowerShell prompt, people ask me how it's put together:
There's a lot going on in this screenshot, so I'm going to put together the pieces. The information here assumes you have general familiarity with customing your PowerShell environment (for example, editing your profile file). It also assumes you have some pre-requisites installed, like Git for Windows and Visual Studio.
You can copy/paste along as we go, but I have also provided downloadable versions at the end of the blog post.
Important Note: When saving the PowerShell files, make sure they are saved as "UTF-8 with Signature". This will ensure that PowerShell treats your file as UTF-8 and does not accidentally treat it as ANSI. Here is an example using my editor of choice, Notepad2:
The first thing you'll notice is that I'm using a tabbed command window. The program I'm using is called ConEmu, and I've chosen it because its configuration allows me to get exactly what I want, in a portable executable. (I keep most of my utilities like this in the cloud, so that I can just sync them down to a new machine with no installation.)
The most critical feature that I'm leveraging here is the ability of ConEmu to create a slightly compressed font display without needing an actual condensed font. Speaking of fonts, I use a font from the awesome Nerd Fonts project to provide several of the custom symbols in my prompt. It joins together icons from several projects (including Powerline, Font Awesome, and Octicons), and creates pre-merged versions of their font based on several free, fixed pitch fonts (my prompt is using Ubuntu Mono).
I get the slightly condensed look with the "Width" setting (width of 9 against font size of 20).
Lastly, I use a custom color set (based heavily on the Ubuntu default terminal colors). The colors I've chosen for many of the prompt elements are based on the mix of these custom colors; if you choose to stick with the default color values, you may wish to adjust some of my color choices for better legibility.
Important note: Because there are use of Unicode characters in many of the
samples, please make sure your text editor is capable of editing files in UTF-8 mode. If you
? characters where you expect to see custom symbols, make sure that your
file is in UTF-8 mode, and that your text editor is using your custom Nerd Font.
The blue portion of the prompt is Git project information, powered by posh-git. This is a PowerShell plugin that supports printing information about the Git repository referenced by the current directory. The great thing about posh-git is that it comes with a tremendous amount of flexibility when formatting the prompt output. Here is what it looks like by default:
You configure the prompt by overriding the default values in a configuration object that posh-git sets. Here are the overrides which convert that default prompt into the blue prompt you've seen above:
To use these overrides, place these lines after the
Import-Module statement that
posh-git added to your PowerShell profile. Once you restart your shell, you should see your new
customizations in place (don't worry about the path; we'll be removing that later on):
If you want to customize the symbols used in the prompt, the easiest way to find them is by using the Character Map application built into Windows. Just select the Nerd Font you're using while browsing characters, and you can copy them directly from this app into your profile:
When PowerShell wants to display your prompt, it executes the command
prompt. By default, this
command is provided by a "PowerShell function". Adding this to your Profile will override the default
prompt with our custom prompt (I'll break down each section afterward):
Let's break this down into the individual parts of the prompt.
First, you may have noticed in my original screenshot that I don't put the current path on the prompt;
instead, I put it into the window title. I find that keeps the prompt compact and consistent. The first
section of my prompt function grabs the current path, and replaces your home folder with
stashing away the value into
$title so it can be used at the end of the prompt:
The second thing we do is determine whether you're running as admin or not (meaning, did your prompt
request administrator rights via UAC). We convert this into a color, so we print the trailing
PS< in either green (safe, non-admin) or red (danger, admin) so you can quickly
see whether your command prompt can get you into extra trouble:
Now we're going to start printing pieces of the prompt. The first thing we're going to do is print out the last exit code, if there was a failure. We also clear it, so that we only end up printing it once (PowerShell won't clear it unless you run another external command, so this helps us keep the prompt clean):
We haven't seen the use of the prompt environment in code yet, but this is where we add something to prompt to indicate we're in a special environment. The example shown up top shows a prompt for Visual Studio 2017; I'll show you the full mechanics of that command later, but the key part here is that the script will set a value that we look for:
Now we'll print the status of our source control (from posh-git), if our current directory happens to be a Git folder:
In PowerShell (as with many other shells), you can use the
commands to temporarily change your current directory, and then pop back to where you started.
This is a stack, which means you can push arbitrary folders to be popped later. Our prompt prints
+ to represent the current stack depth:
Here is a quick screenshot of how this works:
There is an oddity with PowerShell's expectations of the
prompt command: it expects
it to return a string, which it then prints. If you fail to return a string, then it will print
PS> for you. Since we already printed that (in our color of choice), we just
return a single space:
The final piece of the puzzle is printing the prompt environment. I have several commands which
place the shell into a special mode to be used for specific environments. For example, I have
a command called
vs2017 which adds Visual Studio environment variables just as
though you'd run the Visual Studio Command Prompt, and then adds that information to the
printed prompt environment.
Here is the contents of
This script is a little complex because Visual Studio 2017 now allows you to install multiple editions
side by side. It looks to see which edition you have installed and uses the
to get the updated environment variables. If you have multiple editions installed, you can use
-edition switch to specify which environment you want.
The final line of the script sets the global
PromptEnvironment variable, which we use
in our custom prompt function.
I have several of these little scripts to set up different environments. You can use this Visual Studio 2017 script as a starting point to developing your own custom environment scripts. In the downloads below, I have provided versions for Visual Studio 2015, 2017, and Preview.
I hope this quick tour through my custom prompt inspires you to make customizations of your own, based on your needs. Being able to carefully craft your prompt to be succinct and quickly glanceable can be very handy when spending significant time in your command window of choice.