-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanipulate-path.ps1
209 lines (177 loc) · 6.23 KB
/
manipulate-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
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
# Tools to manipulate your system-wide and user-specific PATH
#
# You can add PATH entries to the top or bottom of the list, or remove extra ones
#
# To manipulate user's PATH:
# user_top(@("c:\path", "c:\path2"))
# user_bottom(@("c:\path", "c:\path2"))
# user_remove(@("c:\path", "c:\path2"))
#
# To manipulate system's PATH:
# system_top(@("c:\path", "c:\path2"))
# system_bottom(@("c:\path", "c:\path2"))
# system_remove(@("c:\path", "c:\path2"))
#
# If the paths already exist in the list, the existing entries are removed, so you
# don't have to worry about first removing duplicates.
#
# The comparison is case-insensitive, so c:\path is the same as C:\Path
#
# Better examples of usage at the bottom of the file
#
# ---------------- #
# Define functions #
# ---------------- #
# Add/move items to the start of a list
function list_top([string[]]$topItems, [string[]]$list) {
# Filter away entry items, just in case
$topItems = $topItems | Where-Object {$_}
$list = $list | Where-Object {$_}
$newList = New-Object System.Collections.ArrayList
# Add desired items to the top
if ($topItems) {
foreach ($item in $topItems) {
$newList.Add($item.Trim()) | Out-Null
}
}
# Then the rest
if ($list) {
foreach ($item in $list) {
# Skip items already added to the top
if ($topItems) {
if ($topItems.Contains($item)) {
continue
}
}
$newList.Add($item.Trim()) | Out-Null
}
}
return $newList
}
# Add/move items to the bottom of a list
function list_bottom([string[]]$bottomItems, [string[]]$list) {
# Filter away entry items, just in case
$bottomItems = $bottomItems | Where-Object {$_}
$list = $list | Where-Object {$_}
$newList = New-Object System.Collections.ArrayList
# Add everything else
if ($list) {
foreach ($item in $list) {
# Skip items going to the bottom
if ($bottomItems) {
if ($bottomItems.Contains($item)) {
continue
}
}
$newList.Add($item.Trim()) | Out-Null
}
}
# Add desired items to bottom
if ($bottomItems) {
foreach ($item in $bottomItems) {
$newList.Add($item.Trim()) | Out-Null
}
}
return $newList
}
# Remove items from a list
function list_remove([string[]]$removeItems, [string[]]$list) {
# Filter away entry items, just in case
$removeItems = $removeItems | Where-Object {$_}
$list = $list | Where-Object {$_}
$newList = New-Object System.Collections.ArrayList
if ($list) {
foreach ($item in $list) {
if ($removeItems) {
if ($removeItems.Contains($item)) {
continue
}
}
$newList.Add($item.Trim()) | Out-Null
}
}
return $newList
}
# Add/move paths to the top of user's PATH
function user_top([string[]]$paths) {
$reg = "Registry::HKCU\Environment"
$oldPath = (Get-ItemProperty -Path "$reg" -Name PATH).Path
$newPath = (list_top $paths $oldPath.Split(";")) -Join ";"
Set-ItemProperty -Path "$reg" -Name PATH -Value "$newPath"
}
# Add/move paths to the bottom of user's PATH
function user_bottom([string[]]$paths) {
$reg = "Registry::HKCU\Environment"
$oldPath = (Get-ItemProperty -Path "$reg" -Name PATH).Path
$newPath = (list_bottom $paths $oldPath.Split(";")) -Join ";"
Set-ItemProperty -Path "$reg" -Name PATH -Value "$newPath"
}
# Remove paths from user's PATH
function user_remove([string[]]$paths) {
$reg = "Registry::HKCU\Environment"
$oldPath = (Get-ItemProperty -Path "$reg" -Name PATH).Path
$newPath = (list_remove $paths $oldPath.Split(";")) -Join ";"
Set-ItemProperty -Path "$reg" -Name PATH -Value "$newPath"
}
# Add/move paths to the top of system-wide PATH
function system_top([string[]]$paths) {
$reg = "Registry::HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
$oldPath = (Get-ItemProperty -Path "$reg" -Name PATH).Path
$newPath = (list_top $paths $oldPath.Split(";")) -Join ";"
Set-ItemProperty -Path "$reg" -Name PATH -Value "$newPath"
}
# Add/move paths to the bottom of system-wide PATH
function system_bottom([string[]]$paths) {
$reg = "Registry::HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
$oldPath = (Get-ItemProperty -Path "$reg" -Name PATH).Path
$newPath = (list_bottom $paths $oldPath.Split(";")) -Join ";"
Set-ItemProperty -Path "$reg" -Name PATH -Value "$newPath"
}
# Remove paths from system-wide PATH
function system_remove([string[]]$paths) {
$reg = "Registry::HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
$oldPath = (Get-ItemProperty -Path "$reg" -Name PATH).Path
$newPath = (list_remove $paths $oldPath.Split(";")) -Join ";"
Set-ItemProperty -Path "$reg" -Name PATH -Value "$newPath"
}
# ----------------- #
# Functions defined #
# ----------------- #
# -------- #
# Examples #
# -------- #
#
# You may or may not want to use these as-is
# They are here just for demonstration purposes
#
# Figure out latest Java JRE version installed
$jre = ((Get-ChildItem "C:\Program Files\Java\" -Filter "jre*") | Sort-Object)[-1]
system_top(@(
"C:\ProgramData\chocolatey\bin",
"C:\Program Files\Java\$jre\bin",
"" # To make it so entries above can all have trailing commas
))
system_bottom(@(
"C:\Windows",
"" # To make it so entries above can all have trailing commas
))
system_remove(@(
"C:\Bad\App"
"" # To make it so entries above can all have trailing commas
))
user_top(@(
"$env:USERPROFILE\bin",
"C:\tools\cygwin\sbin",
"C:\tools\cygwin\usr\sbin",
"C:\tools\cygwin\bin",
"C:\tools\cygwin\usr\local\bin",
"" # To make it so entries above can all have trailing commas
))
user_bottom(@(
"C:\Low\Prio"
"" # To make it so entries above can all have trailing commas
))
user_remove(@(
"C:\Bad\App"
"" # To make it so entries above can all have trailing commas
))