简体   繁体   中英

Windows PowerShell: changing the command prompt

Using Windows PowerShell, how do I change the command prompt?

For example, the default prompt says

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

I want to customize that string.

Just put the function prompt in your PowerShell profile ( notepad $PROFILE ), eg:

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

如果您Set-Location为网络共享,此版本的沃伦史蒂文斯的答案可避免路径中出现嘈杂的“Microsoft.PowerShell.Core\\FileSystem”。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM