-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Get-Path.ps1
137 lines (118 loc) · 4 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
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
<#
.SYNOPSIS
Display the PATH environment variable as a list of strings rathan than a single string
and displays the source of each value defined in the Registry: Machine, User, or Process
.PARAMETER search
An optional search string to find in each path.
.PARAMETER sort
Sorts the strings alphabetically, otherwise displays them in the order in which
they appear in the PATH environment variable
.DESCRIPTION
Reports whether each path references an existing directory, if it is duplicated in
the PATH environment variable, if it is and empty entry. See the Repair-Path command
for a description of how it cleans up the PATH. Also reports the PATH length and
warns when it exceeds 80% capacity.
#>
# CmdletBinding adds -Verbose functionality, SupportsShouldProcess adds -WhatIf
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[string] $search,
[switch] $sort
)
Begin
{
function ExpandPath ($path)
{
# check env variables in path like '%USREPROFILE%'
$match = [Regex]::Match($path, '\%(.+)\%')
if ($match.Success)
{
$evar = [Environment]::GetEnvironmentVariable( `
$match.Value.Substring(1, $match.Value.Length - 2))
if ($evar -and ($evar.Length -gt 0))
{
return $path -replace $match.value, $evar
}
}
return $path
}
}
Process
{
# In order to avoid substitution of environment variables in path strings
# we must pull the Path property raw values directly from the Registry.
# Other mechanisms such as [Env]::GetEnvVar... will expand variables.
$0 = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
$sysKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($0, $false <# readonly #>)
$sysPaths = $sysKey.GetValue('Path', $null, 'DoNotExpandEnvironmentNames') -split ';'
$sysExpos = $sysPaths | ? { $_ -match '\%.+\%' } | % { ExpandPath $_ }
$sysKey.Dispose()
$usrKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $false <# readonly #>)
$usrPaths = $usrKey.GetValue('Path', $null, 'DoNotExpandEnvironmentNames') -split ';'
$usrExpos = $usrPaths | ? { $_ -match '\%.+\%' } | % { ExpandPath $_ }
$usrKey.Dispose()
if ($VerbosePreference -eq 'Continue')
{
Write-Host 'Original System Paths' -ForegroundColor DarkYellow
Write-Host ($sysPaths -join [Environment]::NewLine) -ForegroundColor DarkGray
Write-Host 'Original User Paths' -ForegroundColor DarkYellow
Write-Host ($usrPaths -join [Environment]::NewLine) -ForegroundColor DarkGray
Write-Host
}
if ($sort) { $paths = $env:Path -split ';' | sort }
else { $paths = $env:Path -split ';' }
$duplicates = @()
Write-Host
$format = "{0,4} {1}"
foreach ($path in $paths)
{
$source = ''
if (($sysExpos -contains $path) -or ($sysPaths -contains $path)) { $source += 'M' }
if (($usrExpos -contains $path) -or ($usrPaths -contains $path)) { $source += 'U' }
if ($source -eq '') { $source += 'P' }
if ($path.Length -eq 0)
{
Write-Host ' -- EMPTY --' -ForegroundColor Yellow
}
elseif ($duplicates.Contains($path))
{
Write-Host("$format ** DUPLICATE" -f $source, $path) -ForegroundColor Yellow
}
else
{
if (!(Test-Path $path))
{
Write-Host("$format ** NOT FOUND" -f $source, $path) -ForegroundColor Red
}
elseif ($search -and $path.ToLower().Contains($search.ToLower()))
{
Write-Host($format -f $source, $path) -ForegroundColor Green
}
else
{
if ($source.Contains('P'))
{
Write-Host($format -f $source, $path) -ForegroundColor White
}
elseif ($source.Contains('U'))
{
if ($usrExpos -contains $path) { $source = "*$source" }
Write-Host($format -f $source, $path) -ForegroundColor Gray
}
else
{
if ($sysExpos -contains $path) { $source = "*$source" }
Write-Host($format -f $source, $path) -ForegroundColor DarkGray
}
}
}
$duplicates += $path
}
Write-Host
Write-Host "PATH contains $($env:Path.Length) bytes" -NoNewline
if (($env:Path).Length -gt ([Int16]::MaxValue * 0.80))
{
Write-Host ' .. exceeds 80% capacity; consider removing unused entries' -ForegroundColor Red
}
Write-Host
}