Add specific folders and subfolders to Windows local profile PATH
environment variable with exclusions.
- Save
add-paths-to-env.ps1
to folder of with subfolders you want add toPATH
. - Edit lines 6, 7 and 12.
- Run:
.\add-paths-to-env.ps1
- Restart.
# Get the script directory | |
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition | |
# Write-Host $scriptDirectory | |
# Add folders | |
$foldersToInclude = @("\bin", "\my-tool").ForEach({ $scriptDirectory + $_ }) | |
$subfoldersToIncludeInFolder = @("\portable").ForEach({ $scriptDirectory + $_ }) | |
Write-Host "foldersToInclude" $foldersToInclude | |
# Define the list of strings to exclude | |
$excludeStrings = @("dict-pl", "Documents", "PortableApps") | |
# Get all folder paths in the script directory, excluding those containing any of the specific strings | |
$portableFolderPaths = $subfoldersToIncludeInFolder | ForEach-Object { | |
Get-ChildItem -Path $_ -Directory | |
} | |
# Write-Host $portableFolderPaths | |
$folderPaths = $portableFolderPaths | Where-Object { | |
$folderPath = $_.FullName | |
$exclude = $false | |
foreach ($excludeString in $excludeStrings) { | |
if ($folderPath -match $excludeString) { | |
$exclude = $true | |
break | |
} | |
} | |
-not $exclude | |
} | | |
Select-Object -ExpandProperty FullName | |
# Write-Host $folderPaths | |
$folderPaths = $foldersToInclude + $folderPaths | |
# Save folder paths to a variable | |
$allFolderPaths = $folderPaths -join "`r`n" | |
Write-Host $allFolderPaths | |
# Source https://stackoverflow.com/a/48624689/1722542 | |
function Add-EnvPath { | |
param( | |
[Parameter(Mandatory = $true)] | |
[string] $Path, | |
[ValidateSet('Machine', 'User', 'Session')] | |
[string] $Container = 'Session', | |
[Parameter(Mandatory = $False)] | |
[Switch] $Prepend | |
) | |
if (Test-Path -path "$Path") { | |
if ($Container -ne 'Session') { | |
$containerMapping = @{ | |
Machine = [EnvironmentVariableTarget]::Machine | |
User = [EnvironmentVariableTarget]::User | |
} | |
$containerType = $containerMapping[$Container] | |
$persistedPaths = [Environment]::GetEnvironmentVariable('Path', $containerType) -split ';' | |
if ($persistedPaths -notcontains $Path) { | |
if ($Prepend) { | |
$persistedPaths = , $Path + $persistedPaths | Where-Object { $_ } | |
[Environment]::SetEnvironmentVariable('Path', $persistedPaths -join ';', $containerType) | |
} | |
else { | |
$persistedPaths = $persistedPaths + $Path | Where-Object { $_ } | |
[Environment]::SetEnvironmentVariable('Path', $persistedPaths -join ';', $containerType) | |
} | |
} | |
} | |
$envPaths = $env:Path -split ';' | |
if ($envPaths -notcontains $Path) { | |
if ($Prepend) { | |
$envPaths = , $Path + $envPaths | Where-Object { $_ } | |
$env:Path = $envPaths -join ';' | |
} | |
else { | |
$envPaths = $envPaths + $Path | Where-Object { $_ } | |
$env:Path = $envPaths -join ';' | |
} | |
Write-Host "Added folder:" $Path | |
} | |
else { | |
Write-Host "Folder already in PATH:" $Path | |
} | |
} | |
} | |
Write-Host "Adding paths to PATH`r`n" | |
foreach ($folderPath in $folderPaths) { | |
Add-EnvPath $folderPath 'User' | |
} |