Customize the PowerShell prompt

1 min read
Note on

I’ve been using the PowerShell Core on Windows Terminal for a while. I wanted to customize the prompt to display > on a newline and the current Git branch along with the working directory. You can do this by overriding the Prompt function and saving it in one of the profiles. Here’s the snippet that I’m using:

powershell
function prompt {
  "`n$env:Username@$env:ComputerName $($executionContext.SessionState.Path.CurrentLocation) $(git branch --show-current)`n$('>' * ($nestedPromptLevel + 1)) "
}

To have a colorful prompt, you can use VT Sequences as follows:

powershell
function prompt {
  "`n`e[32m$env:Username@$env:ComputerName`e[0m `e[34m$($executionContext.SessionState.Path.CurrentLocation)`e[0m `e[33m$(git branch --show-current)`e[0m`n$('>' * ($nestedPromptLevel + 1)) "
}

I was not familiar with VT Sequences but this Stack Overflow post helped me figure it out.

Share