Show Current Git Branch in PowerShell

If you happen to switch between branches a lot, or find yourself doing git branch to see what branch you are currently on, you can easily modify your PowerShell profile to show your current active branch right in the prompt. Here is how.

Step 1

Open powershell profile for edit by entering “notepad $profile” in powershell. This will open your PowerShell profile located here: C:\Users\yourname\Documents\WindowsPowerShell

Step 2

Add the following function in the profile definition.

function prompt {
$host.ui.rawui.WindowTitle = $(get-location)

$prompt_string = “PS ” + $(get-location) + ” [”

if(Test-Path .git) {
git branch | foreach {
if ($_ -match “^\*(.*)”){
$prompt_string += $matches[1] + ” ]> ”
}
}
}
else{
$prompt_string = “PS> ”
}

Write-Host ($prompt_string) -nonewline -foregroundcolor yellow
return ” ”
}

Step 3

Save and re-open the powershell. Your prompt should now show as following in yellow with branch name showing in the square brackets:

PS C:\Projects\foo [ x.x.currentBranch ]>

Show Current Git Branch in PowerShell