Skip to content

Instantly share code, notes, and snippets.

@drazul
Last active April 25, 2024 09:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save drazul/b92f780689bd89a0d2a7 to your computer and use it in GitHub Desktop.
Save drazul/b92f780689bd89a0d2a7 to your computer and use it in GitHub Desktop.
Example to add and remove local-path to system-path-variable on Windows. Without the limitations of 1024 characters
#------------ Add path to system variable -------------------------------------
$path2add = ';C:\path;'
$systemPath = [Environment]::GetEnvironmentVariable('Path', 'machine');
If (!$systemPath.contains($path2add)) {
$systemPath += $path2add
$systemPath = $systemPath -join ';'
[Environment]::SetEnvironmentVariable('Path', $systemPath, 'Machine');
write-host "Added to path!"
write-host $systemPath
}
#------------ Delete path from system variable --------------------------------
$path2delete = 'C:\path;'
$systemPath = [Environment]::GetEnvironmentVariable('Path', 'machine');
$systemPath = $systemPath.replace($path2delete, '')
$systemPath = $systemPath -join ';'
[Environment]::SetEnvironmentVariable('Path', $systemPath, 'Machine');
write-host "Deleted from path!"
write-host $systemPath
#------------ Clean system variable -------------------------------------------
$systemPath = [Environment]::GetEnvironmentVariable('Path', 'machine');
while ($systemPath.contains(';;')) {
$systemPath = $systemPath.replace(';;', ';')
}
[Environment]::SetEnvironmentVariable('Path', $systemPath, 'Machine');
write-host "Cleaned path!"
write-host $systemPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment