-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows-system-paths.ps1
222 lines (184 loc) · 6.36 KB
/
windows-system-paths.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
<# Powershell 2.0 Script #>
## Functions
# return if a directory is in the path via system or user registry hives (or both)
function pathcheck {
[Cmdletbinding()]
param(
[parameter(Mandatory=$true, Position=1)]
[string]$folder
)
$folder = $folder.TrimEnd('\')
if (($userpath -contains $folder) -and ($syspath -contains $folder)) { "both"
} elseif ($userpath -contains $folder) { "user"
} elseif ($syspath -contains $folder) { "system"
} else { $false }
}
# return if a binary is first in the PATH -or- if the fully qualified name exists
# else return False
function bincheck {
[Cmdletbinding()]
param(
[parameter(Mandatory=$true, Position=1)]
[string]$binary
)
$exec = Split-Path -leaf $binary
$gcm = $(Get-Command $exec -ErrorAction SilentlyContinue)
if ($gcm) {
[string]$gcm.source.ToLower()
} else {
if (Test-Path $binary) {
[string]$binary.ToLower()
} else {
$false
}
}
}
# return candidate "Program Files" names for a specified binary
function wow64 {
[Cmdletbinding()]
param(
[parameter(Mandatory=$true, Position=1)]
[string]$partialpath
)
[string[]] $paths = @()
if (${Env:ProgramFiles(x86)}) {
$paths = $paths + "${Env:ProgramFiles(x86)}\$partialpath"
}
$paths = $paths + "${Env:ProgramFiles}\$partialpath"
$paths.ToLower()
}
# remember that the user path is appended to the system path.
# reset the path.
$env:PATH = [System.Environment]::GetEnvironmentVariable("Path","Machine") +
";" + [System.Environment]::GetEnvironmentVariable("Path","User")
# pick up 'heavyweight' .Net ArrayList so we can _remove_ things
$syspath = New-Object System.Collections.Generic.List[System.Object]
ForEach ($dir in $([System.Environment]::GetEnvironmentVariable("Path","Machine").split(";").TrimEnd('\'))) {
$syspath.Add($dir.ToLower())
}
$userpath = New-Object System.Collections.Generic.List[System.Object]
if ([System.Environment]::GetEnvironmentVariable("Path","User")) {
ForEach ($dir in $([System.Environment]::GetEnvironmentVariable("Path","User").split(";").TrimEnd('\'))) {
$userpath.Add($dir.ToLower())
}
}
$userpath.Add("c:\program files\git\cmd".ToLower())
write-host "starting with expanded PATH $env:PATH"
# array of all the _possible_ paths to add
$paths = @()
# GnuPG - this code sets the path and GPG utility environment.
$gpgs = @()
$gpgs = $gpgs + $(wow64 "GNU\GnuPG\pub\gpg.exe")
ForEach ($candidate in $gpgs) {
$found = $(bincheck $candidate)
if ($found) {
$paths = $paths + $($found | Split-Path -Parent)
if (-not "${env:GNUPGHOME}") {
# call gpg to create a homedir...
& gpg -K | Out-Null
if (Test-Path "${env:APPDATA}\GnuPG") {
New-ItemProperty -Path HKCU:\Environment -Name GNUPGHOME -Value "%APPDATA%\GnuPG" -PropertyType ExpandString -Force | Out-Null
}
}
}
}
# Putty/plink - this code also configures the git ssh transport
$plinks = @()
$plinks = $putties + $(wow64 "PuTTY\plink.exe")
ForEach ($candidate in $plinks) {
$found = $(bincheck $candidate)
if ($found) {
$paths = $paths + $($found | Split-Path -Parent)
if (-not "${env:GIT_SSH}") {
New-ItemProperty -Path HKCU:\Environment -Name GIT_SSH -Value "plink.exe" -PropertyType ExpandString -Force | Out-Null
}
}
}
# Editplus - also configure EDITOR
$eps = @()
$eps = $eps + $(wow64 "EditPlus\editplus.exe")
ForEach ($candidate in $eps) {
$found = $(bincheck $candidate)
if ($found) {
$paths = $paths + $($found | Split-Path -Parent)
if (-not "${env:EDITOR}") {
New-ItemProperty -Path HKCU:\Environment -Name EDITOR -Value "editplus.exe" -PropertyType ExpandString -Force | Out-Null
}
}
}
# git
$gits = @()
$gits = $gits + $(wow64 "Git\cmd\git.exe")
ForEach ($candidate in $gits) {
$found = $(bincheck $candidate)
if ($found) {
$paths = $paths + $($found | Split-Path -Parent)
}
}
# loop through $paths now for adding to path
ForEach ($path in $($paths | Get-Unique)) {
$pc = pathcheck $path
if (-not $pc) {
$userpath.add($path)
}
if ($pc -eq "both") {
$userpath.remove($path) | Out-Null
}
}
# well known expansions
$evars = @{
# actually, don't add systemroot it's super obnoxious and special-cased out
# "%SystemRoot%" = "$($env:SystemRoot.ToLower())\";
"%ProgramFiles(x86)%" = "$(${env:ProgramFiles(x86)}.ToLower())\";
"%ProgramFiles%" = "$($env:ProgramFiles.ToLower())\"
}
# copy to new envs
$_syspath = New-Object System.Collections.Generic.List[System.Object]
$_userpath = New-Object System.Collections.Generic.List[System.Object]
$sr = $env:SystemRoot.ToLower()
# loop through $paths substituting...
ForEach ($component in $userpath) {
$_userpath.add($component.Replace($sr,"%SystemRoot%"))
ForEach ($ekey in $evars.keys) {
$esz = "$($evars.Item($ekey))"
if ($component -like "$esz*") {
$nc = $component.Replace($esz,"${ekey}\")
$_userpath.remove($component) | Out-Null
$_userpath.add($nc)
}
}
}
ForEach ($component in $syspath) {
$_syspath.add($component.Replace($sr,"%SystemRoot%"))
ForEach ($ekey in $evars.keys) {
$esz = "$($evars.Item($ekey))"
if ($component -like "$esz*") {
$nc = $component.Replace($esz,"${ekey}\")
$_syspath.remove($component) | Out-Null
$_syspath.add($nc)
}
}
}
$_userpath = $_userpath | sort-object | get-unique
$_syspath = $_syspath | sort-object | get-unique
# for system paths, move %systemroot% to the front again
$_fsyspath = New-Object System.Collections.Generic.List[System.Object]
$_psyspath = New-Object System.Collections.Generic.List[System.Object]
ForEach ($component in $_syspath) {
if ($component -like "%SystemRoot%*") {
$_psyspath.Add($component)
} else {
$_fsyspath.Insert(0,$component)
}
}
$finsyspath = $_psyspath + $_fsyspath
# okay enough of that. make some strings.
$user_sz_path = $_userpath -join ";"
$sys_sz_path = $finsyspath -join ";"
write-host "setting User path to"
write-host "$user_sz_path"
New-ItemProperty -Path HKCU:\Environment -Name PATH -Value "$user_sz_path" -PropertyType ExpandString -Force | Out-Null
write-host "setting system path to "
write-host "$sys_sz_path"
New-ItemProperty -PATH "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" `
-Name Path -Value "sys_sz_path" -PropertyType ExpandString -Force | Out-Null