I'm using the Prompt function to use a custom prompt. I've got it so I get the date, current working directory and number of objects. Where I am in the $scripts
or $modules
locations, I'd like the current working directory to truncate.
$scripts = "$(Split-Path $profile)\Scripts"
$modules = "$(Split-Path $profile)\Modules"
where the part of the Prompt function responsible is this:
Write-Host ($PWD) -NoNewline -ForegroundColor Gray
Perhaps you're looking for something like this:
$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
or like this:
$pattern = [regex]::Escape((Split-Path $profile)) + '\\((Scripts|Modules)(\\.*|$))'
Write-Host ($PWD.Path -replace $pattern, '~\$1') -NoNewline -ForegroundColor Gray