Skip to content

My Ideal Powershell Prompt with Git Integration

I have become quite fond of Powershell lately and enjoy its flexibility. As part of this flexibility, it has impressive scope for customisation and modification similar to Bash (although arguably in a much nicer way). My main bugbear with the default installation of Powershell is the horrible prompt you are provided with. As such, I have modified my prompt to better suit my needs including useful details about the git repository when within one.

The Git part of my prompt is inspired by this blog post, but with some of my own ideas and tweaks. It's also worth noting that like others, I use the excellent Console app, which makes a much nicer host application for Powershell (and cmd.exe, bash, etc).

So What Does It Look Like?

My standard (non-git) prompt looks like this:

I decided to make it closer resemble the standard bash prompt as I find it useful. It shows my username, the machine name and the current folder I am inside. This is good for me as I regularly SSH into other machines (which have a similar prompt) and this makes it easy to see at a glance what machine I am connected to and where on the filesystem I am at present. I also find it useful to set the title to the same information, as can be seen in the tab at the top of the Console window.

When I move into a git repository I have cloned (or created) on my machine, the prompt grows to show me some useful information about my repository clone:

In addition to my standard prompt, I now have the git branch name and number of files added (+), edited (~) and deleted (-) shown. The branch name also changes colour depending on the status: if I have made no commits since cloning (i.e. my branch is not ahead of that on origin), the branch name is shown in cyan. If I have made additional commits (and my branch is therefore ahead of that on origin), the branch name changes to red. This is to remind me to push my branch back up to origin when I am done. Of course, if there is no remote, it will never change colour. Also, if additional files have been created but are not yet tracked, an exclamation point (!) will appear at the end of the prompt to show the presence of untracked files.

Nuts and Bolts

So now onto the good stuff - how it's done. Powershell uses a profile.ps1 (or Microsoft.Powershell_profile.ps1) file within your ~/Documents/WindowsPowershell directory to determine what happens to your Powershell. It is pretty much equivalent to Bash's ~/bash_profile file. In order to change the prompt, we simply create a function called prompt, and do whatever we want in here.

I have split mine into two files: profile.ps1 which defines the prompt function and actually decides what should be shown, and gitutils.ps1 which contains a couple of functions for gathering the git information. I have made them both available as gists on GitHub and these can be dropped straight into your ~/Documents/WindowsPowershell directory.

The Code

gitutils.ps1

profile.ps1

Summary

So there it is - my ideal fusion of Powershell, Git and the spirit of bash. Of course, my gigantic prompt might not suit all - I like it as it contains a lot of information, but it also uses a lot of screen space. The prompt function can be tweaked to shrink it up if necessary - the username@host part might not be for everyone.

You may have also noticed in my screenshots that all the colours appear a bit more pastel than normal - Console allows you to tweak the colours which are used for each of the 16 ANSI colours. However I did make sure it looks fine with the default Powershell colours too, and it does. Enjoy.