PowerShell - my prompt

Customizing the PowerShell Prompt

By default, PowerShell comes with a prompt that can be annoying at times. Thankfully, customizing the PowerShell prompt is a relatively easy task.

The main reason that I dislike the default PowerShell prompt is because it displays the full path to your current directory. When working deep within nested folders, the prompt begins to take up a lot of the horizontal line space. This means that even short commands can run onto the next line, making it more awkward to read.

default powershell prompt

Customizing the PowerShell prompt is easy. You just need to define a function call “prompt” and store this in your PowerShell profile.

Getting Started

The first thing to do is find your PowerShell profile. You can do this by simply entering $profile into a PowerShell session and pressing return. This will give you the path to your profile file.

It’s worth noting that different terminal emulators can use different prompt files. For example, the profile file referred to by PowerShell ISE is not the same profile file that Windows Terminal uses. However you can create a prompt function in both if needed.

The profile file can be edited by running “notepad $prompt” as shown below:

PowerShell - displaying the location of your profile

Running the above command will launch notepad and will likely open an empty file. Within the file, you can define a function called “prompt”.

NotePad - example prompt function

The image above shows my PowerShell prompt function, which displays a few basic pieces of information:

  • Current date (not actually displayed, but used for getting the time)
  • The time in the format I want, using GetDateTimeFormats
  • Current working directory’s name (not the full path)
  • If the current directory is the root of a drive (e.g. C:\) then I format the string to make it display nicer. By default, it would just show as “C”

From there, the function then writes this information to the host, which creates the new PowerShell prompt.

For simplicity, I use the Write-Host cmdlet. This makes it easy to the change text colours and background colours of the prompt.

The Finished Result

Once the prompt function has been defined, save the changes and restart the PowerShell session. You should now see the prompt you have defined.

PowerShell - my prompt

My prompt might not be the most aesthetically pleasing, but it gets the job done. The use of colours makes it easy to see where commands begin when scrolling back through the session.

In the blue section, I display my current username and hostname. This can be a useful reminder of who you are and where you are if you have multiple user accounts on multiple machines.

The yellow section displays the name of the current working directory. Unlike the default prompt, it does not display the absolute path to this directory.

The magenta section displays the time. This just gives me a rough idea of when I executed a command and how long it took to run (if it took more than 1 minute).

If you ever want to revert back to the default prompt, simple edit the profile file again and delete your function. You will need to start a new PowerShell session for the changes to be applied.

At this point you should have enough information to start customizing your own PowerShell prompts. If you would like to play around with my prompt then feel free to copy the code below.

Example prompt function

function prompt {
    $date = Get-Date
    $time = $date.GetDateTimeFormats()[88]
    $curdir = $ExecutionContext.SessionState.Path.CurrentLocation.Path.Split("\")[-1]

    if($curdir.Length -eq 0) {
        $curdir = $ExecutionContext.SessionState.Drive.Current.Name+":\"
    }

    Write-Host ""$env:USERNAME"@"$env:COMPUTERNAME" " -BackgroundColor Cyan -ForegroundColor Black -NoNewline
    Write-Host " DIR:"$curdir" " -BackgroundColor Yellow -ForegroundColor Black -NoNewline
    Write-Host ""$time" " -BackgroundColor Magenta -ForegroundColor White
    "> "
}

Check out Microsoft’s documentation on the PowerShell prompt if you would like more detailed information.

2 thoughts on “Customizing the PowerShell Prompt

  1. Thanks.

    There’s a small typo in the Getting started section. You reference $prompt in the text where you mean to type $profile…

Leave a Reply

Your email address will not be published. Required fields are marked *