-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd2UserPathPermanently.ps1
More file actions
120 lines (118 loc) · 4.42 KB
/
add2UserPathPermanently.ps1
File metadata and controls
120 lines (118 loc) · 4.42 KB
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
# add2UserPathPermanently.ps1
# This script adds specified directories to the user's PATH environment variable permanently.
param (
[string[]]$DirectoriesToAdd = @(
"$env:USERPROFILE\bin",
"$env:USERPROFILE\Documents\GitHub\PowerShell-Scripts\bin"
),
[string[]]$SubDirectoriesToAdd = @(
"$env:USERPROFILE\AppData\Local\Microsoft\WinGet\Packages\",
"$env:USERPROFILE\AppData\Local\Programs",
"$env:USERPROFILE\AppData\Roaming\Programs",
"C:\Programme",
"C:\ProgrammsZiped",
"C:\ZipedPrograms",
"C:\Programs"
)
)
# Enable debug output
$debug = $false # $true
Write-Host "Directories to add:`n$($DirectoriesToAdd -join "`n")`n"
Write-Host "SubDirectories to add:`n$($SubDirectoriesToAdd -join "`n")`n"
# extend DirectoriesToAdd with scanning for directories containing executables in SubDirectoriesToAdd
foreach ($subDir in $SubDirectoriesToAdd) {
# Get all directories in the specified subdirectory that contain executables
if ($debug) {
Write-Host "Scanning subdirectory: $subDir"
}
# skip if $env:ProgramFiles
if ($subDir -eq $env:ProgramFiles -or $subDir -eq ${env:ProgramFiles(x86)} ) {
if ($debug) {
Write-Host "Skipping system Program Files directory: $subDir"
}
continue
}
# skip if too much direct entries
$directEntries = Get-ChildItem -Path $subDir -Directory -ErrorAction SilentlyContinue
if ($directEntries.Count -gt 42) {
if ($debug) {
Write-Host "Skipping $subDir due to large number of entries: $($directEntries.Count)"
}
continue
}
# skip if dir doesn't exist
if (-not (Test-Path -Path $subDir)) {
if ($debug) {
Write-Host "Subdirectory does not exist, skipping: $subDir"
}
continue
}
$foundDirs = Get-ChildItem -Path $subDir -Directory -Recurse -Depth 1 | Where-Object {
$_.GetFiles("*.exe", [System.IO.SearchOption]::AllDirectories).Count -gt 0
}
foreach ($dir in $foundDirs) {
# skip if name contains ~
if ($dir.Name -match "~") {
if ($debug) {
Write-Host "Skipping directory with ~ in name: $($dir.FullName)"
}
continue
}
# skip if match [0-9]+(\.[0-9]+)+
if ($dir.Name -match "^[0-9]+(\.[0-9]+)+$") {
if ($debug) {
Write-Host "Skipping version-like directory: $($dir.FullName)"
}
continue
}
# skip xxx|yyy|zzz
if ($dir.Name -match "^(autoupdate|lib|dependencies|uninst|resources)$") {
if ($debug) {
Write-Host "Skipping directory: $($dir.FullName)"
}
continue
}
$DirectoriesToAdd += $dir.FullName
}
}
# Combine the main directories with the subdirectories
# Get the current user's PATH environment variable
$currentPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::User)
# Split the current PATH into an array
$pathArray = $currentPath -split ';'
if ($debug) {
Write-Host "Current PATH:`n$currentPath`n"
Write-Host "PATH as array:`n$($pathArray -join "`n")`n"
}
# Add each directory to the PATH if it doesn't already exist
foreach ($directory in $DirectoriesToAdd) {
# check if directory exists
if ($debug) {
Write-Host "Processing directory: $directory"
Write-Host -NoNewline "exists:`t"
Test-Path -Path $directory
Write-Host -NoNewline "in path:`t"
$pathArray -contains $directory
}
# skip if not exists
if (-not (Test-Path -Path $directory)) {
Write-Host "Directory $directory does not exist, skipping."
continue
}
if ( ($pathArray -contains $directory)) {
Write-Host "$directory is already in PATH, skipping."
} else {
$pathArray += $directory
Write-Host "Adding $directory to PATH."
}
}
# Join the updated array back into a single string
$newPath = $pathArray -join ';'
# Set the updated PATH environment variable permanently for the user
[Environment]::SetEnvironmentVariable("Path", $newPath, [EnvironmentVariableTarget]::User)
Write-Host "Updated PATH environment variable for the user."
# Optionally, you can refresh the environment variables in the current session
$env:Path = $newPath
Write-Host "Current session PATH updated."
# End of script
# Note: This script requires administrative privileges to run successfully.