How-to: Set the PowerShell Prompt.

The default PowerShell prompt displays the current working directory.

To display the prompt definition:
(Get-Command prompt).definition

The prompt function can be changed by creating a function called 'prompt' this can be just for the current session, or if saved in your profile will apply to all future sessions.

A simple prompt showing the current location:

function prompt { 'PS ' + $(get-location) + '> ' }

or using the automatic variable $pwd

function prompt { 'PS ' + $pwd + '> ' }

Display only the current folder instead of the full path (via Larry Weiss)

function prompt {
'PS ' + ($pwd -split '\\')[0]+' '+$(($pwd -split '\\')[-1] -join '\') + '> '
}

Display a different prompt when logged in as an Administrator, note this calculates the Administrator membership once (in the PowerShell profile) rather than every time the prompt() function is run:

$Global:Admin=''
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = new-object System.Security.principal.windowsprincipal($CurrentUser)
if ($principal.IsInRole("Administrators")) { $Admin='ADMIN ' }

Function prompt { $Admin + "PS $(get-location)> " }

Window Title

The Window Title can also be modified as part of the prompt definition. By default this is the username followed by "Windows PowerShell"

$host.ui.rawui.WindowTitle = "String you want to show on the title bar"

Display the current location in the title bar:

$host.ui.rawui.WindowTitle = "PS $pwd"

To display the current Host settings including WindowTitle:

$ (Get-Host).UI.RawUI

“When my mother gets a prompt 'Do you want to download this?' she’s going to say yes. It’s disingenuous for Microsoft to give you all of these tools with which to hang yourself, and when you do, then say it’s your fault” ~ Bruce Schneier

Related PowerShell Cmdlets

PowerShell Functions


 
Copyright © 1999-2024 SS64.com
Some rights reserved