Powershell이 ​​cmd 프롬프트를 상속받게 하는 방법이 있습니까?

Powershell이 ​​cmd 프롬프트를 상속받게 하는 방법이 있습니까?

cmd 쉘에서는 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>

관련 정보