Windows PowerShell: 更改命令提示符

使用 Windows PowerShell,如何更改命令提示符?

例如,默认提示符说

PS C:\Documents and Settings\govendes\My Documents>

我想定制这个字符串。

64718 次浏览

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.)

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!

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...

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>"}

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! :)