-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_folders_to_path.ps1
98 lines (89 loc) · 4.72 KB
/
add_folders_to_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
param(
[switch]$commit = $false
)
# Allow easy customization of colors
function Format-Color([hashtable] $Colors = @{}, [switch] $SimpleMatch) {
$lines = ($input | Out-String) -replace "`r", "" -split "`n"
foreach($line in $lines) {
$color = ''
foreach($pattern in $Colors.Keys){
if(!$SimpleMatch -and $line -match $pattern) { $color = $Colors[$pattern] }
elseif ($SimpleMatch -and $line -like $pattern) { $color = $Colors[$pattern] }
}
if($color) {
Write-Host -ForegroundColor $color $line
} else {
Write-Host $line
}
}
}
$folders = Get-ChildItem -Path . -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName
$oldPath = [Environment]::GetEnvironmentVariable('PATH', 'User');
$escapedPath = [RegEx]::Escape($oldPath)
Write-Host ""
Write-Host "==================================================================================================================================="
Write-Host "Your PATH is currently set to: ";
Write-Host "==================================================================================================================================="
$oldPath
Write-Host ""
Write-Host "==================================================================================================================================="
Write-Host "The following folders were found in the working directory and will be added to your PATH: ";
Write-Host "==================================================================================================================================="
$outputTable = @()
$alreadyExistsString = "Already exists and will NOT be added to PATH."
$willBeAddedString = "Will be added to PATH."
Foreach($folder in $folders){
if($oldPath -Match [RegEx]::Escape($folder.FullName)){
$entry = [PSCustomObject]@{
Path = $folder.FullName
Status = $alreadyExistsString
}
$outputTable += $entry
}
else {
$entry = [PSCustomObject]@{
Path = $folder.FullName
Status = $willBeAddedString
}
$outputTable += $entry
}
}
$outputTable | Format-Table -AutoSize | Format-Color @{$willBeAddedString = 'Green'; $alreadyExistsString = 'Red'}
if(!$commit){
Write-Host "===================================================================================================================================" -ForegroundColor Yellow
Write-Warning "This was a test run. In order to commit these results to your path, call the script again with the '-commit' flag."
Write-Host "===================================================================================================================================" -ForegroundColor Yellow
}
else {
$addedOutputTable = @()
$successString = "Successfully added to PATH."
$updatedPath = $oldPath
Foreach($folder in $folders){
if(!($oldPath -Match [RegEx]::Escape($folder.FullName))){
$updatedPath += ";$($folder.FullName)"
$entry = [PSCustomObject]@{
Path = $folder.FullName
Status = $successString
}
$addedOutputTable += $entry
}
}
Write-Host "==================================================================================================================================="
Write-Host "The following folders were added to your PATH:";
Write-Host "==================================================================================================================================="
$addedOutputTable | Format-Table -AutoSize | Format-Color @{$successString = 'Green'}
[Environment]::SetEnvironmentVariable("PATH", $updatedPath, [EnvironmentVariableTarget]::User)
$newPath = [Environment]::GetEnvironmentVariable('PATH', 'User');
Write-Host "===================================================================================================================================" -ForegroundColor Cyan
Write-Host "Your PATH has been updated to: " -ForegroundColor Cyan
Write-Host "===================================================================================================================================" -ForegroundColor Cyan
$newPath
Write-Host ""
Write-Host "===================================================================================================================================" -ForegroundColor Yellow
Write-Host "Your previous PATH was written to 'old_path.txt' as a precaution. Delete this file if it is not needed for a restoration." -ForegroundColor Yellow
Write-Host "===================================================================================================================================" -ForegroundColor Yellow
$oldPath | Out-File .\old_path.txt
Write-Host ""
Write-Host "PATH updated successfully!" -ForegroundColor Green
Write-Host ""
}