I used "setx" to add a new path to my PATH environment variable. How do I see whether we can delete the newly added path from PATH environment variable?
-
Look here: https://technet.microsoft.com/en-us/library/ff730964.aspx – Olcay Ertaş Aug 18 '16 at 06:17
-
1@OlcayErtaş That just covers removing entire environment variables, not removing a single item from the value of a variable like PATH. – mikemaccana Jul 24 '17 at 12:53
-
Page contains instructions to remove only a specific item: `Remove-Item Env:\TestVariable` – Olcay Ertaş Jul 24 '17 at 13:06
3 Answers
Deleting a specific value from %PATH% needs you to get the variable, modify it, and put it back.
For example.
# Get it
$path = [System.Environment]::GetEnvironmentVariable(
'PATH',
'Machine'
)
# Remove unwanted elements
$path = ($path.Split(';') | Where-Object { $_ -ne 'ValueToRemove' }) -join ';'
# Set it
[System.Environment]::SetEnvironmentVariable(
'PATH',
$path,
'Machine'
)

- 3,910
- 15
- 15
-
4Users may also be interested in replacing `$_` in answer with `$_.TrimEnd('\')` as sometimes paths have trailing slash and sometimes they don't – bbodenmiller Feb 19 '21 at 09:31
-
After running these commands, $env.path is not updated and `get-command` still finds an executable in the path I've deleted. If I run the "get it" command again I can see the path is removed. Is there something I need to do to update the $env ? – andrew lorien Oct 06 '22 at 02:25
-
I would like to provide an update to this post. Just changing the $Env:Path
or using [System.Environment]::SetEnvironmentVariable()
will not permanently change the path variable as per earlier post by Rich (maybe Windows changed that since the original post). It will only change it for that session in powershell. Once you exit and restart powershell, the path will revert back. In order to change it permanently in powershell, you have to change the value in the registry.
Here's another way to modify the registry (can be modified to fit a new needed path).
#Path of the directory you want to add
$newPath = 'C:\folder\another folder\etc'
#Gets the original value from the registry
$oldPath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
#Sets the registry to the new value. NOTE: You have to get the old path, otherwise if you just use set-itemproperty with $newPath, it sets it just to that new path, erasing the previous path
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value "$oldPath$newPath;"
#Semi-colon at the end is to keep the consistency of the path variable
#You can double-check it worked using the Environment Variables GUI
This information can also be found at the following external website: https://codingbee.net/powershell/powershell-make-a-permanent-change-to-the-path-environment-variable

- 1
- 1
- 4
Chris' answer does not persist after a reboot. For it to work after a reboot, you need to modify the registry location of PATH. Here is a function example for both removing an item from path and adding an item:
# Modify PATH variable
function changePath ($action, $addendum) {
$regLocation =
"Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment"
$path = (Get-ItemProperty -Path $regLocation -Name PATH).path
# Add an item to PATH
if ($action -eq "add") {
$path = "$path;$addendum"
Set-ItemProperty -Path $regLocation -Name PATH -Value $path
}
# Remove an item from PATH
if ($action -eq "remove") {
$path = ($path.Split(';') | Where-Object { $_ -ne "$addendum" }) -join ';'
Set-ItemProperty -Path $regLocation -Name PATH -Value $path
}
}
# Add an item to your path
changePath "add" "C:\example"
# Remove an item from your path
changePath "remove" "C:\example"
-
9Chris' answer _does_ persist, because he uses `Machine` as the scope in the `[System.Environment]::SetEnvironmentVariable()` call, which updates the registry behind the scenes (the same goes for `User`, which updates the current user's definition; only using `Process` is non-persistent). Not only is use of `[System.Environment]::SetEnvironmentVariable()` more convenient than modifying the registry directly, it also notifies other applications of the persistent change via a `WM_SETTINGCHANGE` message. – mklement0 Feb 06 '18 at 16:20