Windows PowerShell: changing the command prompt

前端 未结 6 813
渐次进展
渐次进展 2020-11-30 20:27

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

For example, the default prompt says

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


        
6条回答
  •  自闭症患者
    2020-11-30 21:03

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

提交回复
热议问题