All modern shells and command lines offer a prompt that provides the user with essential information about his context. By default, however, PowerShell displays only the current directory. Since its appearance is determined by a function, you can change the PowerShell prompt easily.

In the old command interpreter cmd.exe, the customization of the prompt is limited to the use of some predefined variables. PowerShell, on the other hand, defines a function called prompt() that defines the appearance of the prompt. By default, this function looks like this:

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

If you open a remote session, Enter-PSSession inserts the name of the remote computer at the beginning of the prompt. However, changing the prompt is not limited to cmdlets. You can also do so by simply overriding the prompt() function.

Change the prompt function

It is a good idea not to rewrite the prompt() function from scratch but to change it. If you want to avoid showing the active debug mode and instead display the current time in front of the prompt, you can do so as follows:

function prompt {
    'PS [' + $(Get-Date -Format "HH:mm") + '] ' + $(Get-Location) +
        $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
} 

Alternatively, you can display the name of the current user. A good way to do so is to read the USERNAME environment variable:

function prompt {
"[$env:USERNAME] " + 'PS ' + $(Get-Location) +
        $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
}
Transfer current time or username to the prompt

Transfer current time or username to the prompt

Change colors

A popular way to customize the prompt is to change its color. This can be achieved by using escape sequences. The following example modifies the function so that the prompt is displayed in red (code 31):

function prompt {
    "$([char]27)[31m PS [" + $(Get-Date -Format "HH:mm") + '] ' + $(Get-Location) +
        $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
} 

The color scheme is not just a matter of taste but can also indicate useful information.For example, you could color the prompt whenever a user is logged in with administrative privileges. In the following example, the prompt appears in red for an admin and in the usual white for a standard user (code 27):

function prompt {
    [Security.Principal.WindowsPrincipal]$user = [Security.Principal.WindowsIdentity]::GetCurrent();
    if($user.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)){
    "$([char]27)[31m PS [" + $(Get-Date -Format "HH:mm") + '] ' + $(Get-Location) +
        $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
    }
    else{
     "$([char]27)[37m PS [" + $(Get-Date -Format "HH:mm") + '] ' + $(Get-Location) +
        $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
    }
}
Customize prompt colors using escape sequences

Customize prompt colors using escape sequences

Not all PowerShell hosts support terminal sequences, so you have to find another solution in this case. This applies to the PowerShell ISE, for example. There, you can change colors for admins versus standard users with Write-Host:

function prompt {
    [Security.Principal.WindowsPrincipal]$user = [Security.Principal.WindowsIdentity]::GetCurrent();
    if($user.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)){
    Write-Host -NoNewline -ForegroundColor Red $('[' + $(Get-Date -Format "HH:mm") + '] ' + $(Get-Location) + " " )
    }
    else{
    Write-Host -NoNewline -ForegroundColor White $('[' + $(Get-Date -Format "HH:mm") + '] ' + $(Get-Location) + " " )
    }
} 
Color the prompt for administrative users in the PowerShell ISE

Color the prompt for administrative users in the PowerShell ISE

In principle, you can use any cmdlet or variable for designing the prompt beyond these examples, if this seems useful to you.

Permanent change only via profiles

When you overwrite the prompt() function, keep in mind that this always affects only the current session. If you start a new session, you will get the usual prompt again.

If you want to keep the PowerShell prompt changes permanently, you have to add the modified version of prompt() to the PowerShell profile.

Summary

In contrast to cmd.exe, where you have to rely on a few predefined variables to change the prompt, PowerShell offers numerous methods to include information in the prompt. Of course, this raises the question of whether the result always makes sense.

Subscribe to 4sysops newsletter!

For example, colors can be used for visual gimmicks, but they can also provide useful information about the current context. However, the escape sequences are not supported on all PowerShell hosts, so you must resort to Write-Host instead.

0 Comments

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account