【问题标题】:Windows PowerShell: changing the command promptWindows PowerShell:更改命令提示符
【发布时间】:2022-01-25 18:31:41
【问题描述】:

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

例如,默认提示说

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

我想自定义那个字符串。

【问题讨论】:

    标签: windows powershell


    【解决方案1】:

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

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

    或彩色:

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

    【讨论】:

    • notepad $PROFILE 在管理员 powershell 提示符下无法在 Windows 7 中工作
    • 啊,我看你需要先创建配置文件:new-item -itemtype file -path $profile -force
    • 注意:您可以将提示功能粘贴到 powershell 中以更改提示路径,而不是将功能保存在配置文件中,但每次启动 powershell 时都必须这样做。
    • 您还需要以管理员身份运行 Powershell 并执行Set-ExecutionPolicy RemoteSigned
    • @qed Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted 如果您只想为当前用户更改或无法以管理员身份运行。
    【解决方案2】:

    Ocaso Protal's answer 的评论相关,Windows Server 2012 和 Windows 7(在 PowerShell 窗口中)需要以下内容:

    new-item -itemtype file -path $profile -force
    notepad $PROFILE
    

    如果您使用多个用户名(例如您自己 + 生产登录名)运行,我会建议以下提示:

    function Global:prompt {"PS [$Env:username]$PWD`n>"} 
    

    (这要归功于 David I. McIntosh。)

    【讨论】:

    • 您还需要以管理员身份运行 Powershell 并执行Set-ExecutionPolicy RemoteSigned
    【解决方案3】:

    如果您想自己做,那么Ocaso Protal's answer 是您的最佳选择。但如果你像我一样懒惰,只想为你做点什么,那么我强烈推荐Luke Sampson's Pshazz package

    只是为了让你看看你有多懒,我将提供一个快速教程。

    • 使用Scoop (scoop install pshazz) 安装 Pshazz
    • 使用漂亮的预定义主题 (pshazz use msys)
    • 喝(根)啤酒

    Pshazz 还允许您创建自己的主题,这与配置 JSON 文件一样简单。 Check out mine 看看有多简单!

    【讨论】:

      【解决方案4】:

      在提示符下,我喜欢网络驱动器的当前时间戳和已解析的驱动器号。为了使其更具可读性,我把它分成两行,并在颜色上玩了一下。

      使用 CMD,我最终得到了

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

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

      这更具可读性:-)

      顺便说一句:

      • 我更喜欢powershell_ise.exe $PROFILE 而不是(愚蠢的)Notepad
      • 如果你想用断点调试你的prompt(),你应该将prompt-function 重命名为其他任何东西(或者在另一个文件中尝试)。否则你可能会陷入一个循环:当你停止调试时,prompt() 再次被调用并且你再次在断点处停止。一开始很烦人……

      【讨论】:

        【解决方案5】:

        如果您 Set-Location 到网络共享,此版本的 Warren Stevens' answer 可避免路径中出现嘈杂的“Microsoft.PowerShell.Core\FileSystem”。

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

        【讨论】:

          【解决方案6】:

          只显示我使用的驱动器号:

          function prompt {(get-location).drive.name+"\...>"}
          

          然后恢复到我使用的路径:

          function prompt {"$pwd>"}
          

          【讨论】: