
I just stumbled upon a necessity to add a directory to my Windows PATH variable. I usually do this using the GUI method which is quite easy.
But I thought it would be great and faster if I’m able to add it directly from my already open PowerShell.
Without further ado, here is how to add new directory to PATH variable using PowerShell:
Warning: the steps below involves modifying Windows registry. Your OS might be damaged if you change the registry incorrectly. Please be careful when following the tutorial.
- Open PowerShell with administrator rights. Right-click the PowerShell shortcut/icon and click Run as administrator. You can also do this via Windows Terminal.
- Check the directory you want to add first and make sure it doesn’t exist in your PATH yet:
($env:path).split(";")
- Run this command to get the old PATH:
$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
- Add the new PATH:
$newpath = "$oldpath;C:\new\path"
Notice the “;” between the old path and the new path. It’s necessary to add it to prevent the new path interfering the old path.
- Apply the new path and set in the PATH variable:
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newpath
- Close and reopen your PowerShell.
- Check the new PATH and make sure the new entry has been added:
($env:path).split(";")
That’s it! Although arguably adding a new entry to the PATH variable would be easier using GUI, adding it via PowerShell is also quite straightforward.
Caution! Don’t use the code above! Path is of type REG_EXPAND_SZ both in HKLM and HKCU, which means that it can contain other environment variables as “templates”. I use this a lot, my (user) Path variable looks like this: “%PYTHONHOME%;%GITHOME%\bin;%PERLHOME%\bin;…”, so I can change the Path any time by only changing some of those other variables like PYTHONHOME.
But the code above would *destroy* all existing template variables contained in Path, by replacing them with their current values, so the user losers the intended functionality! That’s because Get-ItemPropertyValue unfortunately always expands such template variables on the fly.
Therefor I strongly recommend to use this instead:
# for Path in user space (HKCU)
$oldpath = (get-item “HKCU:\Environment”).GetValue(“Path”, $null, ‘DoNotExpandEnvironmentNames’)
# for Path in global space (HKLM)
$oldpath = (get-item “HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment”).GetValue(“Path”, $null, ‘DoNotExpandEnvironmentNames’)
Thanks for the heads up! Good to know there’s a better alternative that can keep template variables in the path.
> ($env:path).split(“;”)
This is not robust. The PATH may contain quoted strings that contain semicolons.
It shouldn’t be a problem as we only use it to check for existing paths. Could you please share if there’s a more consistent and robust way to get the paths?
This did not change for Powershell when run as user. How do I change for other users too?
You may want to set your path using user profile if the path contains user path: https://superuser.com/a/442163/1434679