有沒有辦法讓Powershell繼承cmd提示符

有沒有辦法讓Powershell繼承cmd提示符

在 cmd shell 中,可以使用 PROMPT 命令等設定(並儲存)提示字元。但是,當您從 cmd 進入 Powershell 時,它會還原到預設的 Windows 提示字元 - 通常是目前目錄。

有沒有辦法強制Powershell使用目前的Windows提示符號?

答案1

感謝非常有用的評論,使這項工作得以實現。以防萬一其他人遇到同樣的問題:

$profile

產生 Powershell 設定檔存在(或將存在)的位置。如果它存在,以下命令將創建它:

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

在這裡,我們只需要寫一個提示函數。我通常將我的設定為當前用戶:

function prompt {"PS: $(echo 'RobbieDee')>"}

答案2

Prompt透過全域設定%UserProfile%\Documents\WindowsPowerShell\profile.ps1

  • 無顏色:

    Function set-prompt {
      "$ESC[$($executionContext.SessionState.Path.CurrentLocation)$('$' * ($nestedPromptLevel + 1)) $ESC[0m"
    }
    
  • 帶顏色:

    switch ($Action) {
    
        "Default" {
            Function global:prompt {
                if (test-path variable:/PSDebugContext) { '[DBG]: ' }
                    write-host " "
                    write-host ("$ESC[48;2;40;40;40m$ESC[38;2;170;210;0m$(Get-Location) $ESC[0m $ESC[0m")
    
                if ( $host.UI.RawUI.WindowTitle -match "Administrator" ) {
                    $Host.UI.RawUI.ForegroundColor = 'Red'
                    $(if ($nestedpromptlevel -ge 1) {
                        write-host ('PS $$ ') -ForegroundColor Red -NoNewLine
                    } else {
                        write-host ('PS $ ') -ForegroundColor Red -NoNewLine
                    })
                } else {
                    $(if ($nestedpromptlevel -ge 1) {
                        write-host ('PS $$ ') -ForegroundColor Blue -NoNewLine
                    } else {
                        write-host ('PS $ ') -ForegroundColor Blue -NoNewLine
                    })
                }
    
                return " "
            }
        }
    }
    
    set-prompt Default
    
    • 向使用者顯示彩色文字提示,向管理員顯示紅色文字提示
    • 允許動態切換設定檔:
      • 複製/貼上Default其下方的部分並進行相應編輯(包括姓名,即Default。透過set-prompt Default||重新載入會話並切換提示符set-prompt <name>

相關內容