PowerShell_Scripts/base/Get-Path.ps1

39 lines
1.2 KiB
PowerShell

[CmdletBinding()]param(
[switch]$Expand,
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,Position=0)]
[Alias("FullName")]
[string[]]$Path
)
process {
foreach( $local:p in $Path ) {
switch( $p[0] ) {
'@' { $p = Join-Path $MyPSScriptRoot $p.Substring(1) }
'~' {
if( $p -match '^~[^/]*' ) {
$local:m = $Matches[0]
$p = $p -replace "^$m",((Get-Item -Force $m).FullName)
}
}
}
# clean end of path from DirectorySeparatorChars
$p = $p -replace '[/\\]+$',''
$p = $p -replace '#C-','#C-#' -replace '\*','#C-A#' -replace '\?','#C-Q#'
if([System.IO.Path]::IsPathRooted($p)){
$p = [System.IO.Path]::GetFullPath($p)
}else{
$p = [System.IO.Path]::GetFullPath((Join-Path $PWD $p))
}
$p = $p -replace '#C-A#','*' -replace '#C-Q#','?' -replace '#C-#','#C-'
if( $p -match '\*|\?' -and $Expand ) {
$p = $p | Where-Object { Test-Path $_ } |
Get-Item -Force |
Select-Object -ExpandProperty FullName
}
$p
}
}