-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathAdd.ps1
More file actions
73 lines (68 loc) · 2.23 KB
/
PathAdd.ps1
File metadata and controls
73 lines (68 loc) · 2.23 KB
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
[CmdletBinding()]
param(
#the path entries to add, or $null to refresh from system
[string[]]$pathAdd,
#User or Machine
[string]$type='User',
#
[string]$PathVariable='PATH',
# skip Test-Path check
[switch]$force,
# re-add
[switch]$ReAdd,
# Add to the top of the path
[switch]$top
)
if ($type -notin 'Local','User','Machine') {throw "Invalid PATH Type Specified (Local,User,Machine)"}
if ($type -eq 'Local') {
$OrigPath=(Get-ChildItem Env:$PathVariable -ErrorAction SilentlyContinue).Value
} else {
$OrigPath=[System.Environment]::GetEnvironmentVariable($PathVariable,$type)
}
$path={$OrigPath -split ';' | Where-Object { $_ -NE ''} | ForEach-Object {$_.TrimEnd('\')}}.Invoke()
$pathAdd=$pathAdd | ForEach-Object {$_.TrimEnd('\')}
if ($ReAdd) {
foreach ($p in $pathAdd) {
while($path.Remove($p)) {}
}
}
foreach ($p in $pathAdd) {
if ($force -or (Test-Path($p))) {
if ($p -notin $path) {
if ($top) {
Write-Verbose "Adding to top: $p"
$path.Insert(0,$p)
} else {
Write-Verbose "Adding: $p"
$path.Add($p)
}
} else {
Write-Verbose "Already in Path: $p"
}
} else {
Write-Warning "Not Exists: $p"
}
}
$NewPath=$path -join ';'
if ($NewPath -ne $OrigPath) {
Write-Verbose "Updating ${type}-${PathVariable}: $NewPath"
if ($type -eq 'Local') {
Set-Item -Path Env:$PathVariable -Value $NewPath
} else {
[System.Environment]::SetEnvironmentVariable($PathVariable,$NewPath,$type)
}
}
if ($type -ne 'Local') {
#env:PATH= machine takes precedence
if ($PathVariable -eq 'PATH') {
Set-Item -Path Env:$PathVariable -Value (
[System.Environment]::GetEnvironmentVariable($PathVariable,'Machine')+';'+
[System.Environment]::GetEnvironmentVariable($PathVariable,'User')
)
} else {
Set-Item -Path Env:$PathVariable -Value (
[System.Environment]::GetEnvironmentVariable($PathVariable,'User')+';'+
[System.Environment]::GetEnvironmentVariable($PathVariable,'Machine')
)
}
}