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?
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 Comments
$_ in answer with $_.TrimEnd('\') as sometimes paths have trailing slash and sometimes they don'tget-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 ?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
Comments
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"
1 Comment
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.
Remove-Item Env:\TestVariable