Skip to content

Instantly share code, notes, and snippets.

@matrixik
Last active August 9, 2023 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matrixik/db386d713c3ecd97d1d9b4cecfbec73a to your computer and use it in GitHub Desktop.
Save matrixik/db386d713c3ecd97d1d9b4cecfbec73a to your computer and use it in GitHub Desktop.
Add specific folders and subfolders to Windows local profile `PATH` environment variable

Add specific folders and subfolders to Windows local profile PATH environment variable with exclusions.

  1. Save add-paths-to-env.ps1 to folder of with subfolders you want add to PATH.
  2. Edit lines 6, 7 and 12.
  3. Run: .\add-paths-to-env.ps1
  4. 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'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment