Using Windows PowerShell, how do I change the command prompt?
For example, the default prompt says
PS C:\D开发者_运维问答ocuments and Settings\govendes\My Documents>
I want to customize that string.
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 " "
}
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.)
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:
- I prefer
powershell_ise.exe $PROFILE
instead of (dumb) Notepad. - If you like to debug your prompt() with breakpoints, you should rename the prompt-function to anything else (or try it in another file). Otherwise you might end up in a loop: When you stop debugging, prompt() is called again and you stop at the breakpoint, again. Quite irritating, at first...
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.
- Install Pshazz with Scoop (
scoop install pshazz
) - Use a nice predefined theme (
pshazz use msys
) - Drink (root) beer
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!
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>"}
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> "}
PROMPT in PowerShell
A better way to track the path, while keeping the hostname and logging time/date in every line run:
function prompt {
$dateTime = get-date -Format "dd.MM.yyyy HH:mm:ss"
$currentDirectory = $(Get-Location)
$UncRoot = $currentDirectory.Drive.DisplayRoot
write-host "$dateTime" -NoNewline -ForegroundColor YELLOW
write-host " $UncRoot" -ForegroundColor White
# Convert-Path needed for pure UNC-locations
write-host "$ENV:COMPUTERNAME-PS:$(Convert-Path $currentDirectory)>" -NoNewline -ForegroundColor GREEN
return " "
}
...and you get:
myservername-C:\Users\myusername\Documents\WindowsPowerShell\scripts>
Finally! :)
精彩评论