如何使powershell缩短“>”之前的长目录

how to make powershell shorten the long directory before ">"

例如,如果我在配置文件中设置别名。ps1

$ws='C:\Users\Jack\folder0\folder1\folder2'

我 cd 到工作区位置后cd $ws

显示如下

PS C:\Users\Jack\folder0\folder1\folder2\>

现在,我想知道是否有办法让它显示如下或类似

PS $ws>

这是我第一次在 Whosebug 上提问。如果有什么不合适的,请多多指教

你可以修改提示功能来做任何你想做的事情。如果只想检查单个变量,可以这样做:

$ws = "c:\users\frode"

function prompt {
    $CurrentLocation = $executionContext.SessionState.Path.CurrentLocation.Path
    if($CurrentLocation -like "$ws*") {
        $CurrentLocation = $CurrentLocation -replace [regex]::Escape($ws), '$ws'
    }

    "PS $($CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

输出:

PS C:\Users> cd .\frode

PS $ws> cd .\Desktop

PS $ws\Desktop> 

如果您需要支持多个变量,您可以将路径存储在哈希表中并进行检查,或者使用 Get-Variable 搜索包含有效路径的变量。请记住排除 ex $PWD,它始终是您当前的位置。