Customizing your PowerShell Prompt

In this post we’ll briefly cover the customization of your PowerShell prompt.

Customizing your PowerShell prompt is important for multiple reasons. One of them being the ability to make your shell yours, whether that’s with different colors, or adding a funny looking prompt. Another being added functionality, like git status, current directory, the time and so on.

In order to modify your prompt you’ll need to edit your profile, by default your user profile should be in $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Now to modify your prompt you’ll need a prompt function in your profile, continue below for a quick example.

Note this will be slightly different if you are using OneDrive to sync your libraries.

Below is a quick example of how to customize your prompt. Feel free to use this as your prompt if you want, however I find half the fun is customizing your own.

function prompt {
    $origLastExitCode = $LastExitCode
    #Is used for displaying git repo status'. Requires Git to be set up.
    Write-VcsStatus

    #This requires a Test-Administrator function.
    if (Test-Administrator) {
        # if elevated
        Write-Host "(Elevated) " -NoNewline -ForegroundColor White
    }

    Write-Host "$env:USERNAME@" -NoNewline -ForegroundColor DarkCyan
    Write-Host "$env:COMPUTERNAME" -NoNewline -ForegroundColor Cyan
    Write-Host " : " -NoNewline -ForegroundColor DarkGray

    $curPath = $ExecutionContext.SessionState.Path.CurrentLocation.Path
    if ($curPath.ToLower().StartsWith($Home.ToLower())) {
        $curPath = "~" + $curPath.SubString($Home.Length)
    }

    Write-Host $curPath -NoNewline -ForegroundColor Blue
    Write-Host " : " -NoNewline -ForegroundColor DarkGray
    Write-Host (Get-Date -Format G) -NoNewline -ForegroundColor DarkCyan
    $LastExitCode = $origLastExitCode
    Write-Host "`n$('<(" <) (> ")>' * ($nestedPromptLevel + 1)) "
}