Skip to content

Instantly share code, notes, and snippets.

@bluearth
Last active January 31, 2021 15:50
Show Gist options
  • Save bluearth/b7eb0ac019ab5e5b840280a902f84329 to your computer and use it in GitHub Desktop.
Save bluearth/b7eb0ac019ab5e5b840280a902f84329 to your computer and use it in GitHub Desktop.
I found my self having to work with powershell (pwsh) quite often. To make things better, I decided to customize my pwsh prompt. Here's how

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:

  1. 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.
  2. Show current path. The need for this is quite obvious and the default pwsh prompt already shows the current path
  3. 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.
  4. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment