I found my self having to work with powershell (pwsh) quite often. To make things better, I decided to customize my pwsh prompt to be able to:
- Show current time. When I'm executing long running task like GAE deployments, I switch to other task or do other things while waiting. When I return to pwsh and the task has completed, having time indicator in the pwsh prompt can help determine when exactly the task has been completed.
- Show current path. The need for this is quite obvious and the default pwsh prompt already shows the current path
- Show current branch if inside git working directory. This feature can be found in git consoles. I want to have similar feature in my pwsh prompt.
- Have all that color coded for visual aid
Here's the documentation on how to play around with pwsh prompt
Here is my prompt
function I ended up with:
function prompt {
# Color definitions
$defaultFg = $([char]27) + '[38;2;255;255;255m' # white
$promptFg = $([char]27) + '[38;2;0;200;0m' # green
$branchFg = $([char]27) + '[38;2;0;200;200m' # cyan
# Debug context indicator
$(if (Test-Path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) +
# PWSH Prompt
$promptFg + 'PWSH' + $defaultFg +
# Date and time
' [' + $(Get-Date -Format "yyyy-MM-dd HH\:mm\:ss") + '] ' +
# Current path
$(Get-Location) +
# Git branch name (if the path is a git repository)
$(if ($(git branch 2>$null)) { " $branchFg(" + (git branch --no-color --show-current) + ")$defaultFg" }) +
# Prompt symbol
$(if ($NestedPromptLevel -ge 1) { '>>' }) + '>' + ' '
}
Now, the only thing left is to have the above script executed everytime pwsh starts. In order to do that, place the script in the Powershell Profile. Use the following command to know where your profile file is:
PS> $Profile
The script may not exists yet. Just create one with the same name if it does not yet exist.