Display Git Branch Name in PowerShell: Easy Setup Guide

PowerShell doesn't show the name of the branch you're working on in a Git project. The native Git Bash terminal does display the branch name, but if you prefer using PowerShell in Windows Terminal, here's what you need to do to add the branch name to the prompt.

  1. Install the posh-git module.
  2. Add the branch name configuration to the PowerShell profile.
  3. Change the execution policy to allow the profile file to run.

Install the posh-git module using the following command:

Install-Module posh-git -Scope CurrentUser -Force

Next, add the following configuration to your PowerShell profile:

function prompt {
    $branch = ''
    if (Test-Path .git) {
        $branch = & git rev-parse --abbrev-ref HEAD 2>$null
        if ($branch) {
            # Add ANSI escape codes for green color
            $branch = "$([char]27)[32m($branch)$([char]27)[0m"
        }
    }
    "$PWD $branch> "
}

If you're not sure where your profile file is, run the command $profile. It will display the full path to your profile file. If the location or file doesn't exist, create them and add the configuration.

$profile

When creating the file, make sure to use the correct filename extension. Avoid creating it through File Explorer; instead, run one of the following commands:

notepad $profile
New-Item -ItemType file -Path $profile

After adding the configuration, the next step is to set the execution policy to allow running scripts:

Set-ExecutionPolicy RemoteSigned CurrentUser

And that’s all you need to do. Just make sure to restart the PowerShell console.