Windows PowerShell:更改命令提示符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5725888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:18:51  来源:igfitidea点击:

Windows PowerShell: changing the command prompt

windowspowershell

提问by sashang

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

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

For example, the default prompt says

例如,默认提示说

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

I want to customize that string.

我想自定义那个字符串。

回答by Ocaso Protal

Just put the function promptin your PowerShell profile (notepad $PROFILE), e.g.:

只需将该函数prompt放在您的 PowerShell 配置文件 ( notepad $PROFILE) 中,例如:

function prompt {"PS: $(get-date)>"}

or colored:

或有色:

function prompt
{
    Write-Host ("PS " + $(get-date) +">") -nonewline -foregroundcolor White
    return " "
}

回答by Warren Stevens

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

与对Ocaso Protal 的回答的评论相关,Windows Server 2012 和 Windows 7(在 PowerShell 窗口中)需要以下内容:

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

(感谢大卫 I. 麦金托什(David I. McIntosh)。)

回答by Trivia Coder

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

使用 CMD,我最终得到了

PROMPT=$E[33m$D$T$H$H$H$S$E[37m$M$_$E[1m$P$G

For PowerShell, I got the same result with:

对于 PowerShell,我得到了相同的结果:

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 $PROFILEinstead 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...
  • 我更喜欢powershell_ise.exe $PROFILE而不是(哑巴)记事本
  • 如果您想使用断点调试 prompt(),您应该将 prompt-function 重命名为其他任何名称(或在另一个文件中尝试)。否则,您可能会陷入循环:当您停止调试时,将再次调用 prompt() 并再次在断点处停止。挺烦人的,一开始...

回答by Fomentia

If you want to do it yourself, then Ocaso Protal's answeris 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.

如果你想自己做,那么Ocaso Protal 的答案就是要走的路。但如果你像我一样懒惰,只想为你做点什么,那么我强烈推荐Luke Sampson 的 Pshazz 包

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
  • 使用Scoop安装 Pshazz ( 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 mineto see how easy it is!

Pshazz 还允许您创建自己的主题,就像配置 JSON 文件一样简单。看看我的,看看它是多么容易!

回答by nudl

This version of Warren Stevens' answeravoids the noisy "Microsoft.PowerShell.Core\FileSystem" in the path if you Set-Locationto network shares.

如果您使用网络共享,此版本的沃伦史蒂文斯的回答避免了路径中嘈杂的“Microsoft.PowerShell.Core\FileSystem” Set-Location

function prompt {"PS [$Env:username@$Env:computername]$($PWD.ProviderPath)`n> "} 

回答by Chris Horan

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