Prettify PowerShell - Customizing the PowerShell Prompt (new Windows Terminal)
Getting away from the default look of a shell, in this case PowerShell, can make your work day a bit brighter (pun intended).
The new prompt function in action:
I am using the new "Windows Terminal" for quite some time now, and I like it. But the default PowerShell theme (yes, I converted from using "cmd" to Powershell) could be a bit more structured and colorful.
(The following instructions mostly work without a powerline font. I recommend using 'Fira Code')
I want to achieve 3 main things regarding the prompt and the terminal window:
- Change the window title to reflect to current working directory
- Structure the prompt to show more relevant information
- Put some colors in 😃
All these changes need to be done in the 'Microsoft.PowerShell_profile.ps1' file in your home directory: C:\Users\<username>\Documents\WindowsPowerShell
There is a function called 'prompt' which can be overridden and customized:
Function prompt
{
return " "
}
Using this function, the prompt can be customized to anyone's liking.
Setting the window title can be achieved with the variable '$Host.UI.RawUI.WindowTitle'. I want to only show the last part of the full-blown absolute path. The following function takes care of that:
Function Set-WindowTitle-PartialPath($path, $depth){
If(Test-Path $path){
$Host.UI.RawUI.WindowTitle = ($path -split "\\")[-$depth..-1] -join "\"
} else {
Write-Warning "$path is not a valid path"
}
}
This function can be combined with the prompt function to set the title on every command:
Function Set-WindowTitle-PartialPath($path, $depth){
If(Test-Path $path){
$Host.UI.RawUI.WindowTitle = ($path -split "\\")[-$depth..-1] -join "\"
} else {
Write-Warning "$path is not a valid path"
}
}
Function prompt{
Set-WindowTitle-PartialPath (Get-Location) 3
}
Adding some structure (date and full path) and colors is a bit trickier because 'Write-Host' can only color the whole string it is printing. We need to split the string up into pieces of the same color and be aware of not printing the new lines:
Write-Host ($(get-location)) -foregroundcolor DarkCyan
Write-Host ("PS") -nonewline -foregroundcolor Magenta
Write-Host (" at ") -nonewline -foregroundcolor White
Write-Host ("(" + $(get-date).Tostring("dd-MM-yy HH:mm:ss") + ")") -nonewline -foregroundcolor Yellow
Write-Host (" |>") -nonewline -foregroundcolor Green
And finally, I want to see the current git branch I am in, if it is a git based repository. The output of 'git status' can be used for that.
$gitstat = (git status) 2>&1
$GIT_BRANCH_GLYPH=[char]0xE0A0
if ($gitstat -notlike "*not a git repo*")
{
Write-Host " on " -nonewline -foregroundcolor White
Write-Host ($GIT_BRANCH_GLYPH + " " + $("$($gitstat)".Split(" ")[2])) -foregroundcolor Magenta
}
else
{
Write-Host ("")
}
The whole profile PowerShell file:
Function Set-WindowTitle-PartialPath($path, $depth){
If(Test-Path $path){
$Host.UI.RawUI.WindowTitle = ($path -split "\\")[-$depth..-1] -join "\"
} else {
Write-Warning "$path is not a valid path"
}
}
Function prompt{
Set-WindowTitle-PartialPath (Get-Location) 3
$gitstat = (git status) 2>&1
$GIT_BRANCH_GLYPH=[char]0xE0A0
Write-Host ""
Write-Host ($(get-location)) -nonewline -foregroundcolor DarkCyan
if ($gitstat -notlike "*not a git repo*")
{
Write-Host " on " -nonewline -foregroundcolor White
Write-Host ($GIT_BRANCH_GLYPH + " " + $("$($gitstat)".Split(" ")[2])) -foregroundcolor Magenta
}
else
{
Write-Host ("")
}
Write-Host ("PS") -nonewline -foregroundcolor Magenta
Write-Host (" at ") -nonewline -foregroundcolor White
Write-Host ("(" + $(get-date).Tostring("dd-MM-yy HH:mm:ss") + ")") -nonewline -foregroundcolor Yellow
Write-Host (" |>") -nonewline -foregroundcolor Green
return " "
}
The new prompt function in action:
