在配置文件提示中使用条件拆分路径

问题描述:

我正在使用提示功能来使用自定义提示。我知道了,所以我得到日期,当前工作目录和对象的数量。我在$scripts$modules位置,我想要当前工作目录截断。在配置文件提示中使用条件拆分路径

$scripts = "$(Split-Path $profile)\Scripts" 
$modules = "$(Split-Path $profile)\Modules" 

其中负责提示功能的部分是这样的:

Write-Host ($PWD) -NoNewline -ForegroundColor Gray 
+0

*我想当前的工作目录截断*截断到什么?你当前的输出是什么,你想输出的结果是什么样的? –

+0

我想要截断删除前缀脚本或模块的用户\用户名\ Documents \ WindowsPowerShell部分 – MaxRussell

+0

请显示您的预期和实际输出。 –

也许你正在寻找的东西是这样的:

$basedir = Split-Path $profile 
$pattern = [regex]::Escape($basedir) + '\\(Scripts|Modules)(\\.*|$)' 
$path = if ($PWD.Path -match $pattern) { 
    $PWD.Path.Replace($basedir, '~') 
} else { 
    $PWD.Path 
} 
Write-Host $path -NoNewline -ForegroundColor Gray 

或像这样:

$pattern = [regex]::Escape((Split-Path $profile)) + '\\((Scripts|Modules)(\\.*|$))' 
Write-Host ($PWD.Path -replace $pattern, '~\$1') -NoNewline -ForegroundColor Gray 
+0

宾果。优秀。 – MaxRussell