derek
derek

Reputation: 10247

PowerShell: how to delete a path in the Path environment variable

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?

Upvotes: 23

Views: 36706

Answers (3)

Dave N
Dave N

Reputation: 1

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

Upvotes: -1

Rich
Rich

Reputation: 7

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"

Upvotes: -2

Chris Dent
Chris Dent

Reputation: 4260

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'
)

Upvotes: 61

Related Questions