Better PowerShell Prompts (Part 1)

by Aug 24, 2022

I know today’s tip isn’t brand new but considering how many people start using PowerShell, it’s worth mentioning again.

By default, the PowerShell prompt shows the current path which can be long and take away lots of screen real estate. A better way is adjusting the prompt. The most fundamental prompt could be looking like this:

function prompt
{
    $Host.UI.RawUI.WindowTitle = Get-Location
    'PS> '
} 

When you run this function, it shortens the prompt to “PS> “ and instead displays the current location in your console or editor title bar. Adjusting the prompt in PowerShell really is done by overwriting the “prompt” function which will be called automatically by PowerShell whenever a command completes.

This is the default prompt logic used by PowerShell if there is no self-defined prompt function:

function prompt {
    $(if (Test-Path variable:/PSDebugContext) { '[DBG]: ' }
      else { '' }) + 'PS ' + $(Get-Location) +
        $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
}

Note however that PowerShell will force a prompt if the “prompt” function does not yield a string – so turning off the prompt altogether neither makes sense nor is easily possible.

If you want to always use your new prompt, make sure your profile script exists (path can be found in $profile), and add the function to your profile script.


Twitter This Tip! ReTweet this Tip!