about_prompts

TOPIC
    about_prompts

SHORT DESCRIPTION
    Describes the Prompt Function and demonstrates how to create a custom
    Prompt Function.

LONG DESCRIPTION
    The Windows PowerShell command prompt indicates that Windows PowerShell
    is ready to run a command:

    PS C:\>

    The Windows PowerShell prompt is determined by the Prompt Function. You
    can customize the prompt by creating your own Prompt Function. Then, you
    can save this Function in your Windows PowerShell profile.

The Prompt Function

     The Prompt Function determines the appearance of the Windows PowerShell
     prompt. Windows PowerShell comes with a built-in Prompt Function, but
     you can override it by defining your own Prompt Function.

     The Prompt Function has the following syntax:

     Function prompt { <function-body> }

     The Prompt Function must return an object, typically a string. We
     recommend that it return a string or an object that is formatted as a
     string. The string should fit on an 80-character line.

     For example:

     PS C:\> Function prompt {“Hello, World > “}
         Hello, World >

     Like all Functions, the Prompt Function is stored in the Function: drive.
     To display the code in the current Prompt Function, type:

     (Get-Item Function:prompt).definition

     This command uses the Get-Item cmdlet to display the Prompt item in the
     Function: drive. Then, it uses dot notation to display the value of the
     Definition property of the Prompt Function.

The Default Prompt

     The default Windows PowerShell prompt is:

     PS>

     This prompt appears only when the prompt Function generates an error or
     when the prompt Function does not return a string or object.

         PS C:\> Function prompt {$null}
         PS>

     Because Windows PowerShell comes with a built-in prompt, you usually do
     not see the default prompt until you write your own prompt Function.

The Built-in Prompt

     Windows PowerShell includes a built-in prompt Function that creates the
     familiar prompts. The built-in prompt Function is:

         Function prompt
         {
             $(if (Test-Path Variable:/PSDebugContext) { ‘[DBG]: ‘ }

             else { ” }) + ‘PS ‘ + $(Get-Location) `

             + $(if ($nestedpromptlevel -ge 1) { ‘>>’ }) + ‘> ‘
         }

     The Function uses the Test-Path cmdlet to determine whether the
     $PSDebugContext automatic Variable is populated. If $PSDebugContext is
     populated, you are in debugging mode, and “[DBG]” is added to the prompt,
     as follows:

     [DBG] PS C:\ps-test>

     If $PSDebugContext is not populated, the Function adds “PS” to the
     prompt. And, the Function uses the Get-Location cmdlet to get the current
     file system directory location. Then, it adds a right angle bracket (>).
     For example:

     PS C:\ps-test>

     If you are in a nested prompt, the Function adds two angle brackets (>>)
     to the prompt. (You are in a nested prompt if the value of the
     $NestedPromptLevel automatic Variable is greater than 1.)

     For example, when you are debugging in a nested prompt, the prompt
     resembles the following prompt:

     [DBG] PS C:\ps-test>>>

     The Enter-PSSession cmdlet prepends the name of the remote computer to
     the current Prompt Function. When you use the Enter-PSSession cmdlet to
     start a session with a remote computer, the command prompt changes to
     include the name of the remote computer. For example:

         PS Hello, World> Enter-PSSession Server01

         [Server01]: PS Hello, World>

     Other Windows PowerShell host applications and alternate shells might
     have their own custom command prompts.

     For more information about the $PSDebugContext and $NestedPromptLevel
     automatic Variables, see about_Automatic_Variables.

Customizing the Prompt

     To customize the prompt, write a new Prompt Function. The Function is not
     protected, so you can overwrite it.

     To write a prompt Function, type the following:

     Function prompt { }

     Then, between the curly braces, enter the commands or the string that
     creates your prompt.

     For example, the following prompt includes your computer name:

     Function prompt {“PS [$env:COMPUTERNAME]> “}

     On the Server01 computer, the prompt resembles the following prompt:

     PS [Server01] >

     The following prompt Function includes the current date and time:

     Function prompt {“$(Get-Date)> “}

     The prompt resembles the following prompt:

     01/01/2008 17:49:47>

     You can also modify the default Prompt Function:

         Function prompt
         {
             $(if (Test-Path Variable:/PSDebugContext) { ‘[DBG]: ‘ }

             else { ” }) + “$(Get-Date)” `

             + $(if ($nestedpromptlevel -ge 1) { ‘>>’ }) + ‘> ‘
         }

     For example, the following modified Prompt Function adds “[ADMIN]:” to
     the built-in Windows PowerShell prompt when Windows PowerShell is opened
     by using the “Run as administrator” option:

         Function prompt
         {
             $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
             $principal = [Security.Principal.WindowsPrincipal] $identity

             $(if (Test-Path Variable:/PSDebugContext) { ‘[DBG]: ‘ }

             elseif($principal.IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”))
                 { “[ADMIN]: ” }

             else { ” }) + ‘PS ‘ + $(Get-Location) + $(if ($nestedpromptlevel -ge 1) { ‘>>’ }) + ‘> ‘
         }

     When you start Windows PowerShell by using the “Run as administrator”
     option, a prompt that resembles the following prompt appears:

     [ADMIN]: PS C:\ps-test>

     The following Prompt Function displays the history ID of the next
     command. To view the command history, use the Get-History
     cmdlet.

         Function prompt
         {
             # The at sign creates an array in case only one history item exists.
             $history = @(Get-History)
             if($history.Count -gt 0)
             {
                $lastItem = $history[$history.Count – 1]
                $lastId = $lastItem.Id
             }

             $nextCommand = $lastId + 1
             $currentDirectory = Get-Location
             “PS: $nextCommand $currentDirectory >”
         }

     The following prompt uses the Write-Host and Get-Random cmdlets to create
     a prompt that changes color randomly. Because Write-Host writes to the
     current host application but does not return an object, this Function
     includes a Return statement. Without it, Windows PowerShell uses the
     default prompt, “PS>”.

         Function prompt
         {
             $color = Get-Random -min 1 -max 16
             Write-Host (“PS ” + $(Get-Location) +”>”) -nonewline -foregroundcolor $color
             return ” ”
         }

Saving the Prompt

     Like any Function, the Prompt Function applies only in the current
     session. To save the Prompt Function for future sessions, add it to your
     Windows PowerShell profiles. For more information about profiles,
     see about_profiles.

SEE ALSO
    Get-Location
    Enter-PSSession
    Get-History
    Get-Random
    Write-Host
    about_profiles
    about_functions
    about_scopes
    about_debuggers
    about_Automatic_Variables