-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystempath.ps1
367 lines (304 loc) · 11 KB
/
systempath.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
class SystemPathLocation {
[ValidateNotNullOrEmpty()] [string] $Location
SystemPathLocation($Location) {
$this.Location = $Location
}
<#
.SYNOPSIS
Folder location in the system path.
.DESCRIPTION
Folder location in the system path.
.EXAMPLE
$location = [SystemPathLocation]::new("C:\Program Files\Git\bin")
#>
}
function Backup-SystemPath {
[CmdletBinding(SupportsShouldProcess)]
param ()
$backupFile = "$env:TEMP\PATH-$(Get-Timestamp).txt"
if ($PSCmdlet.ShouldProcess($backupFile, "Backup system path")) {
$env:PATH > $backupFile
}
<#
.SYNOPSIS
Backs up the system path to a file in the temp folder.
.DESCRIPTION
Backs up the system path to a file in the temp folder.
.EXAMPLE
Backup-SystemPath
#>
}
function local:Add-PathLocation {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $Path,
[Parameter(Mandatory = $true)]
[Alias("Folder")]
[string] $Location,
[Parameter(Mandatory = $true)]
[bool] $Front
)
$oldLocations = $Path -split ";"
foreach ($oldLocation in $oldLocations) {
if ($oldLocation.TrimEnd("\") -ieq $Location.TrimEnd("\")) {
Write-Error "Path already contains location: '$Location'" -ErrorAction Stop
}
}
$pathWithoutSemicolon = $Path.TrimEnd(";")
return $Front ? "$Location;$pathWithoutSemicolon" : "$pathWithoutSemicolon;$Location"
<#
.SYNOPSIS
Adds a location to a semicolon-separated path.
.DESCRIPTION
Permanently adds the specified location to the specified semicolon-separated path and returns the path.
If the path already contains the location,
an error is reported and the execution is stopped.
.PARAMETER Path
Semiocolon separated path to add the location to.
.PARAMETER Location
Folder location to add to the path.
.PARAMETER Front
If specified, the location is added to the beginning of the path.
Otherwise, it is added to the end.
.OUTPUT
Modified path.
.EXAMPLE
Add-PathLocation -Path "C:\Windows;C:\Windows\System32" -Location "C:\Program Files\Git\bin" -Front
.EXAMPLE
Add-PathLocation -Path "C:\Windows;C:\Windows\System32" -Location "C:\Program Files\Git\bin"
#>
}
function local:Remove-PathLocation {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $Path,
[Parameter(Mandatory = $true)]
[Alias("Folder")]
[string] $Location
)
$newPath = $Path -split ";" `
| Where-Object { $_.TrimEnd("\") -ine $Location.TrimEnd("\") } `
| Join-String -Separator ";"
if ($newPath -eq $Path) {
Write-Error "Location not found in path: '$Location'"
}
return $newPath
<#
.SYNOPSIS
Removes a location from a semicolon-separated path and returns the path.
.DESCRIPTION
Removes each occurence of location from the specified semicolon-separated path, if the path contains it.
.PARAMETER Path
Semiocolon separated path to remove the location from.
.PARAMETER Location
Folder location to remove from the path.
Trailing backslashes on the location argument and within the path are ignored.
.RETURN
The path with the location removed.
.EXAMPLE
$newPath = Remove-PathLocation -Path "C:\Windows;C:\Program Files\Git\bin\" -Location "C:\Program Files\Git\bin"
# -> "C:\Windows"
# Note the missing trailing backslash
.EXAMPLE
$newPath = Remove-PathLocation -Path "C:\Windows;C:\Program Files\Git\bin\" -Location "C:\Program Files\Git\bin"
# -> "C:\Windows"
.EXAMPLE
$newPath = Remove-PathLocation -Path "C:\Windows;C:\Windows\System32" -Location "C:\Program Files\Git\bin"
# -> "C:\Windows;C:\Windows\System32"
#>
}
function Get-SystemPath {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ParameterSetName = "Machine")]
[switch] $Machine,
[Parameter(Mandatory = $true, ParameterSetName = "User")]
[switch] $User,
[Parameter(Mandatory = $false, ParameterSetName = "Effective")]
[switch] $Effective,
[Parameter(Mandatory = $false)]
[switch] $Join
)
$params = @{
Name = "Path"
}
if ($Machine -or $User) {
$context = `
if ($Machine) { @{ Machine = $true } } `
elseif ($User) { @{ User = $true } }
$path = Get-EnvironmentVariable @context @params
}
else {
$path = $env:PATH
}
return $Join ? $path :
($path -split ";" | ForEach-Object { if ($_) { [SystemPathLocation]::new($_) } })
<#
.SYNOPSIS
Retrieves the system path.
.DESCRIPTION
Retrieves the system path, either for the current user, for the local machine
or the system path in effect in the current context.
The path is returned as an array of SystemPathLocation objects by default.
If the -Join switch is specified, the path is returned as a semicolon-separated string.
.PARAMETER Machine
If specified, the system path for the local machine is returned.
.PARAMETER User
If specified, the system path for the current user is returned.
.PARAMETER Effective
If specified, the effective system path is returned. The effective system path is the current user path with the local machine path appended to it.
.PARAMETER Join
If specified, the system path is returned as a semicolon-separated string. Otherwise, it is returned as an array of SystemPathLocation objects.
.EXAMPLE
Get-SystemPath
.EXAMPLE
Get-SystemPath -Machine
.EXAMPLE
Get-SystemPath -User -Join
.ALIAS
path
#>
}
New-Alias -Name path -Value Get-SystemPath -ErrorAction SilentlyContinue | Out-Null
function local:Set-SystemPath {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true)]
[string] $Path,
[Parameter(Mandatory = $true, ParameterSetName = "Machine")]
[switch] $Machine,
[Parameter(Mandatory = $true, ParameterSetName = "User")]
[switch] $User
)
if ($Machine) {
Assert-Administrator
}
Backup-SystemPath
$context = `
if ($Machine) { @{ Machine = $true } } `
elseif ($User) { @{ User = $true } }
$params = @{
Name = "Path"
Value = $Path
}
Set-EnvironmentVariable @context @params
<#
.SYNOPSIS
Modifies the system path.
.DESCRIPTION
Sets the system path to the specified path, either for the current user or for the local machine.
.PARAMETER Path
Semiocolon separated path to set.
.PARAMETER Machine
If specified, the system path for the local machine is used.
.PARAMETER User
If specified, the system path for the current user is used.
.EXAMPLE
Set-SystemPath -Path "C:\Windows;C:\Windows\System32"
.EXAMPLE
Set-SystemPath -Path "C:\Windows;C:\Windows\System32" -Machine
.EXAMPLE
Set-SystemPath -Path "C:\Windows;C:\Windows\System32" -User
#>
}
function Add-SystemPathLocation {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Position = 0, Mandatory = $true)]
[Alias("Folder")]
[string] $Location,
[Parameter(Mandatory = $false)]
[Alias("Prepend", "First", "Start")]
[switch] $Front,
[Parameter(Mandatory = $false, ParameterSetName = "Machine")]
[switch] $Machine,
[Parameter(Mandatory = $true, ParameterSetName = "User")]
[switch] $User
)
try {
$context = `
if ($User) { @{ User = $true } } `
else { @{ Machine = $true } }
$params = @{
Path = Add-PathLocation -Path (Get-SystemPath @context -Join) -Location $Location -Front:$Front
}
if ($PSCmdlet.ShouldProcess($Location, "Add location to system path")) {
Set-SystemPath @context @params
# enable new location immediately
$env:PATH = Add-PathLocation -Path "$env:PATH" -Location $Location -Front:$Front
}
}
catch {
Write-Error $_.Exception.Message -ErrorAction Stop
}
<#
.SYNOPSIS
Adds a location to the system path.
.DESCRIPTION
Adds the specified location to the system path, either for the current user or for the local machine, if the path does not contain it.
.PARAMETER Location
Folder location to add to the system path.
.PARAMETER Machine
If specified, the system path for the local machine is used.
.PARAMETER User
If specified, the system path for the current user is used.
.PARAMETER Front
If specified, the location is added to the beginning of the path. Otherwise, it is added to the end.
.EXAMPLE
Add-SystemPathLocation -Location "C:\Program Files\Git\bin"
.EXAMPLE
Add-SystemPathLocation -Location "C:\Program Files\Git\bin" -Machine
.EXAMPLE
Add-SystemPathLocation -Location "C:\Program Files\Git\bin" -User
.EXAMPLE
Add-SystemPathLocation -Location "C:\Program Files\Git\bin" -Front
#>
}
function Remove-SystemPathLocation {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Position = 0, Mandatory = $true)]
[Alias("Folder")]
[string] $Location,
[Parameter(Mandatory = $false, ParameterSetName = "Machine")]
[switch] $Machine,
[Parameter(Mandatory = $true, ParameterSetName = "User")]
[switch] $User
)
try {
$context = `
if ($User) { @{ User = $true } } `
else { @{ Machine = $true } }
$params = @{
Path = Remove-PathLocation -Path (Get-SystemPath @context -Join) -Location $Location -ErrorAction Stop
}
if ($PSCmdlet.ShouldProcess($Location, "Remove location from system path")) {
Set-SystemPath @context @params
# disable new location immediately
$env:PATH = Remove-PathLocation -Path "$env:PATH" -Location $Location
}
}
catch {
Write-Error $_.Exception.Message -ErrorAction Stop
}
<#
.SYNOPSIS
Removes a location from the system path.
.DESCRIPTION
Removes the specified location from the system path, either for the current user or for the local machine, if the path contains it.
.PARAMETER Location
Folder location to remove from the system path.
.PARAMETER Machine
If specified, the system path for the local machine is used.
.PARAMETER User
If specified, the system path for the current user is used.
.EXAMPLE
Remove-SystemPathLocation -Location "C:\Program Files\Git\bin"
.EXAMPLE
Remove-SystemPathLocation -Location "C:\Program Files\Git\bin" -Machine
.EXAMPLE
Remove-SystemPathLocation -Location "C:\Program Files\Git\bin" -User
#>
}