Last active
August 28, 2023 14:38
-
-
Save rezanid/7e1ee12d9005f5900717d0511f70fd05 to your computer and use it in GitHub Desktop.
Adding PATH to Windows permanently (and safely) the right way using PowerShell
This file contains 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 Add-Path { | |
param( | |
[Parameter(Mandatory, Position=0)] | |
[string] $LiteralPath, | |
[ValidateSet('User', 'CurrentUser', 'Machine', 'LocalMachine')] | |
[string] $Scope | |
) | |
Set-StrictMode -Version 1; $ErrorActionPreference = 'Stop' | |
$isMachineLevel = $Scope -in 'Machine', 'LocalMachine' | |
if ($isMachineLevel -and -not $($ErrorActionPreference = 'Continue'; net session 2>$null)) { throw "You must run AS ADMIN to update the machine-level Path environment variable." } | |
$regPath = 'registry::' + ('HKEY_CURRENT_USER\Environment', 'HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment')[$isMachineLevel] | |
# Note the use of the .GetValue() method to ensure that the *unexpanded* value is returned. | |
$currDirs = (Get-Item -LiteralPath $regPath).GetValue('Path', '', 'DoNotExpandEnvironmentNames') -split ';' -ne '' | |
if ($LiteralPath -in $currDirs) { | |
Write-Verbose "Already present in the persistent $(('user', 'machine')[$isMachineLevel])-level Path: $LiteralPath" | |
return | |
} | |
$newValue = ($currDirs + $LiteralPath) -join ';' | |
# Update the registry. | |
Set-ItemProperty -Type ExpandString -LiteralPath $regPath Path $newValue | |
# Broadcast WM_SETTINGCHANGE to get the Windows shell to reload the | |
# updated environment, via a dummy [Environment]::SetEnvironmentVariable() operation. | |
$dummyName = [guid]::NewGuid().ToString() | |
[Environment]::SetEnvironmentVariable($dummyName, 'foo', 'User') | |
[Environment]::SetEnvironmentVariable($dummyName, [NullString]::value, 'User') | |
# Finally, also update the current session's `$env:Path` definition. | |
# Note: For simplicity, we always append to the in-process *composite* value, | |
# even though for a -Scope Machine update this isn't strictly the same. | |
$env:Path = ($env:Path -replace ';$') + ';' + $LiteralPath | |
Write-Verbose "`"$LiteralPath`" successfully appended to the persistent $(('user', 'machine')[$isMachineLevel])-level Path and also the current-process value." | |
} | |
#Original: https://stackoverflow.com/questions/69236623/adding-path-permanently-to-windows-using-powershell-doesnt-appear-to-work |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment