Last active
April 6, 2025 19:49
-
-
Save fdcastel/38ec25c8fc862e691c6d70d95c22fe4b to your computer and use it in GitHub Desktop.
Windows Powershell functions for system path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-SystemPath { | |
$keyName = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment' | |
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($keyName, 'ReadOnly') | |
try { | |
return $key.GetValue('Path', '', 'DoNotExpandEnvironmentNames') -split [IO.Path]::PathSeparator | |
} finally { | |
if ($null -ne $key) { | |
$key.Dispose() | |
} | |
} | |
} | |
function Add-SystemPath([Parameter(Mandatory=$true)][string[]]$Folder) { | |
$keyName = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment' | |
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($keyName, $true) | |
try { | |
# Get current PATH | |
$currentPathFolders = $key.GetValue('Path', '', 'DoNotExpandEnvironmentNames') -split [IO.Path]::PathSeparator | |
# Add new folders to the current PATH | |
$newPathFolders = $currentPathFolders + @($Folder) | |
# Normalize folders to remove trailing slashes and duplicates | |
$result = [Collections.Generic.HashSet[string]]::new([StringComparer]::InvariantCultureIgnoreCase) | |
$newPathFolders | | |
ForEach-Object { | |
$normalized = $_.TrimEnd([IO.Path]::DirectorySeparatorChar).Trim() | |
if ($normalized -ne '') { | |
$result.Add($normalized) | |
} | |
} > $null | |
# Build new PATH and save it | |
$newPath = $result -join [IO.Path]::PathSeparator | |
$key.SetValue('Path', $newPath, 'ExpandString') | |
return $result | |
} finally { | |
if ($null -ne $key) { | |
$key.Dispose() | |
} | |
} | |
} | |
function Remove-SystemPath([Parameter(Mandatory=$true)][string[]]$Folder) { | |
$keyName = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment' | |
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($keyName, $true) | |
try { | |
# Get current PATH | |
$currentPathFolders = $key.GetValue('Path', '', 'DoNotExpandEnvironmentNames') -split [IO.Path]::PathSeparator | |
# Normalize folders to remove | |
$foldersToRemove = $Folder | ForEach-Object { $_.TrimEnd([IO.Path]::DirectorySeparatorChar) } | |
# Filter out the folders to remove (case-insensitive) | |
$result = [Collections.Generic.HashSet[string]]::new([StringComparer]::InvariantCultureIgnoreCase) | |
$currentPathFolders | | |
Where-Object { | |
$normalizedFolder = $_.TrimEnd([IO.Path]::DirectorySeparatorChar) | |
$foldersToRemove -notcontains $normalizedFolder | |
} | | |
ForEach-Object { $result.Add($_) } > $null | |
# Build new PATH and save it | |
$newPath = $result -join [IO.Path]::PathSeparator | |
$key.SetValue('Path', $newPath, 'ExpandString') | |
return $result | |
} finally { | |
if ($null -ne $key) { | |
$key.Dispose() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment