-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tidy-Env.ps1
67 lines (52 loc) · 2.05 KB
/
Tidy-Env.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
## Script to remove any duplicate and inexistent paths from %PATH% (machine and user)
function Remove-MissingPaths($paths) {
$ExistPaths = @()
foreach ($path in $paths) {
if (Test-Path $path -PathType Container) {
$ExistPaths += $path
}
}
Return $ExistPaths
}
function Get-EnvPathArr($Scope) {
$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
}
function Update-EnvPathsIfNeeded($Scope, $paths) {
$result = Compare-Object -ReferenceObject (Get-EnvPathArr($Scope)) -DifferenceObject $paths
if($result -ne $null) {
Write-Output "Updating $Scope %PATH% to $paths"
try {
[Environment]::SetEnvironmentVariable("Path", $paths -join ';', [System.EnvironmentVariableTarget]::$Scope)
}
catch {
$Raise_Error = "ERROR updating $Scope %PATH%"; Throw $Raise_Error
}
return "SUCCESS"
}
return "NOUPDATE"
}
$systEnv = Get-EnvPathArr('Machine')
$userEnv = Get-EnvPathArr('User')
# Remove duplicates
$systEnv = $systEnv | Select-Object -Unique
$userEnv = $userEnv | Select-Object -Unique
# Remove missing paths
$userEnv = Remove-MissingPaths($userEnv)
$systEnv = Remove-MissingPaths($systEnv)
# Remove any user paths that is already in System
$userEnv = Compare-Object -ReferenceObject $systEnv -DifferenceObject $userEnv | Where SideIndicator -eq '=>' | Select -ExpandProperty InputObject
Update-EnvPathsIfNeeded -Scope 'User' -Paths $userEnv
Update-EnvPathsIfNeeded -Scope 'Machine' -Paths $systEnv
# Updating local ENV
$allPaths = (Get-EnvPathArr('All')) -join ';'
if ($allPaths -ne $env:PATH) {
Write-Output 'Updating %PATH% in local shell.'
$env:PATH = $allPaths
}