-
Notifications
You must be signed in to change notification settings - Fork 0
/
Install-Path.ps1
executable file
·120 lines (92 loc) · 3.24 KB
/
Install-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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
## Add script location to %PATH%
param(
[Switch]$Uninstall,
[Switch]$RefreshOnly
)
$actionVerb = ''
$installPaths = @(
# "$PSScriptRoot",
# "$PSScriptRoot\sysinternals",
# "$PSScriptRoot\Hashicorp",
# "$PSScriptRoot\google-cloud-sdk\bin",
# "$PSScriptRoot\Notepad++",
# "$PSScriptRoot\PuTTY",
# "$PSScriptRoot\GitBashPortable",
# "$PSScriptRoot\GitBashPortable\bin",
# "$PSScriptRoot\GitBashPortable\mingw64\bin",
# "$PSScriptRoot\GitBashPortable\usr\bin",
# "$PSScriptRoot\ffmpeg\bin"
)
function Get-EnvPathsArr {
Param(
[ValidateSet('User', 'Machine', 'All')]
$Scope = 'All'
)
$Paths = @()
if ( @('Machine', 'All') -icontains $Scope) {
$Paths += `
[Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine).Split(';', [System.StringSplitOptions]::RemoveEmptyEntries)
}
if ( @('User', 'All') -icontains $Scope) {
$Paths += `
[Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::User).Split(';', [System.StringSplitOptions]::RemoveEmptyEntries)
}
return $Paths
}
Write-Output ''
###
### Install
###
if (!$Uninstall -and !$RefreshOnly) {
$actionVerb = 'INSTALL'
$existingPaths = Get-EnvPathsArr
$existingPathsUser = Get-EnvPathsArr('User')
# List of paths that will be added
$newPaths = Compare-Object -ReferenceObject $existingPaths -DifferenceObject $installPaths | `
where SideIndicator -eq '=>' | `
select -ExpandProperty InputObject
if ($newPaths.Count -gt 0) {
$newEnvTargetUser = ($existingPathsUser + $newPaths) -join ';'
Write-Host "${actionVerb}: Adding the following paths to user %PATH%:`n- $($newPaths -join "`n- ")`n"
[Environment]::SetEnvironmentVariable("Path", "$newEnvTargetUser", [System.EnvironmentVariableTarget]::User)
}
else {
Write-Output "${actionVerb}: Paths already present, no changes needed."
}
}
###
### Uninstall
###
if ($Uninstall -and !$RefereshOnly) {
$actionVerb = 'UNINSTALL'
$existingPathsUser = Get-EnvPathsArr('User')
$newPaths = Compare-Object -ReferenceObject $existingPathsUser -DifferenceObject $installPaths | `
where SideIndicator -eq '<=' | `
select -ExpandProperty InputObject
$removingPaths = Compare-Object -ReferenceObject $existingPathsUser -DifferenceObject $installPaths -ExcludeDifferent -IncludeEqual | `
select -ExpandProperty InputObject
if ($removingPaths.Count -gt 0) {
$newEnvTargetUser = $newPaths -join ';'
Write-Host "${actionVerb}: Removing the following paths from user %PATH%:`n- $($removingPaths -join "`n- ")`n"
Write-Host "${actionVerb}: Updated user %PATH% values:`n- $($newPaths -join "`n- ")`n"
[Environment]::SetEnvironmentVariable("Path", "$newEnvTargetUser", [System.EnvironmentVariableTarget]::User)
}
else {
Write-Output "${actionVerb}: No paths present, no changes needed."
}
}
###
### Refresh Shell
###
if ($RefereshOnly) {
$actionVerb = 'REFRESH-ONLY'
}
$pathsRegistry = $(Get-EnvPathsArr) -join ';'
if ($pathsRegistry -ne $env:Path) {
Write-Output "${actionVerb}: Refreshing %PATH% in current shell..."
$env:Path = $(Get-EnvPathsArr) -join ';'
}
else {
Write-Output "${actionVerb}: %PATH% already up to date."
}
Write-Output ''