-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathGet-Path.ps1
68 lines (56 loc) · 1.73 KB
/
Get-Path.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<#
$Metadata = @{
Title = "Get Path"
Filename = "Get-Path.ps1"
Description = ""
Tags = "powershell, function, Path"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-10-28"
LastEditDate = "2014-01-08"
Version = "1.0.0"
License = @'
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Switzerland License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ch/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
function Get-Path {
<#
.SYNOPSIS
Solves environment variabels, PowerShell variabels in static and relative path strings.
.DESCRIPTION
This function returns a normalized path string of static or relative path which contains environment variabels or PowerShell variabels.
.PARAMETER Path
Path to resolve.
.EXAMPLE
PS C:\> Get-Path "%ProgramFiles(x86)%\Git\bin"
PS C:\> Get-Path "\apps"
PS C:\> Get-Path "$Home"
#>
param(
[Parameter(Mandatory=$true)]
[string]
$Path
)
#--------------------------------------------------#
# main
#--------------------------------------------------#
# evirontment variables
$Path = [System.Environment]::ExpandEnvironmentVariables($Path)
# powershell variables
if($Path.contains("$")){
$Path = Invoke-Expression "`"$Path`""
}
# relative paths
if($Path.StartsWith("\")){$Path = Join-Path -Path $(Get-Location).Path -Childpath $Path}
# folder up execution
$Path = Resolve-Path $Path -ErrorAction SilentlyContinue -ErrorVariable _frperror
if(-not($Path)) {
$Path = $_frperror[0].TargetObject
}
# return path
$Path
}