Using Windows PowerShell, how do I change the command prompt?
For example, the default prompt says
PS C:\\Documents and Settings\\govendes\\My Docume
Just put the function prompt
in your PowerShell profile (notepad $PROFILE
), e.g.:
function prompt {"PS: $(get-date)>"}
or colored:
function prompt
{
Write-Host ("PS " + $(get-date) +">") -nonewline -foregroundcolor White
return " "
}
At the prompt, I like a current timestamp and resolved drive letters for network drives. To make it more readable, I put it in two lines, and played a bit with colors.
With CMD, I ended up with
PROMPT=$E[33m$D$T$H$H$H$S$E[37m$M$_$E[1m$P$G
For PowerShell, I got the same result with:
function prompt {
$dateTime = get-date -Format "dd.MM.yyyy HH:mm:ss"
$currentDirectory = $(Get-Location)
$UncRoot = $currentDirectory.Drive.DisplayRoot
write-host "$dateTime" -NoNewline -ForegroundColor White
write-host " $UncRoot" -ForegroundColor Gray
# Convert-Path needed for pure UNC-locations
write-host "PS $(Convert-Path $currentDirectory)>" -NoNewline -ForegroundColor Yellow
return " "
}
Which is a little more readable :-)
BTW:
powershell_ise.exe $PROFILE
instead of (dumb) Notepad.Related to a comment to Ocaso Protal's answer, the following is needed for Windows Server 2012 as well as Windows 7 (in a PowerShell window):
new-item -itemtype file -path $profile -force
notepad $PROFILE
I would suggest the following as a prompt if you run with multiple user names (e.g. yourself + a production login):
function Global:prompt {"PS [$Env:username]$PWD`n>"}
(Credit goes to David I. McIntosh for this one.)
If you want to do it yourself, then Ocaso Protal's answer is the way to go. But if you're lazy like me and just want something to do it for you, then I highly recommend Luke Sampson's Pshazz package.
Just to show you how lazy you can be, I'll provide a quick tutorial.
scoop install pshazz
)pshazz use msys
)Pshazz also allows you to create your own themes, which is as simple as configuring a JSON file. Check out mine to see how easy it is!
This version of Warren Stevens' answer avoids the noisy "Microsoft.PowerShell.Core\FileSystem" in the path if you Set-Location
to network shares.
function prompt {"PS [$Env:username@$Env:computername]$($PWD.ProviderPath)`n> "}
To just show the drive letter I use:
function prompt {(get-location).drive.name+"\...>"}
Then to revert to the path I use:
function prompt {"$pwd>"}