PowerShell:如何删除Path环境变量中的路径

21

我使用 "setx" 命令将一个新路径添加到我的 PATH 环境变量中。如何查看是否可以从 PATH 环境变量中删除新添加的路径?


请查看此处:https://technet.microsoft.com/zh-cn/library/ff730964.aspx - Olcay Ertaş
1
@OlcayErtaş 这只涵盖了删除整个环境变量,而不是从变量值(如PATH)中删除单个项目。 - mikemaccana
页面包含仅删除特定项的说明:Remove-Item Env:\TestVariable - Olcay Ertaş
3个回答

55

从%PATH%中删除特定值需要获取变量,修改它并将其放回。

例如:

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

4
用户可能会有兴趣将答案中的 $_ 替换为 $_.TrimEnd('\'),因为有时路径末尾带有斜杠,有时则没有。 - bbodenmiller
运行这些命令后,$env.path没有更新,get-command仍然可以在我已删除的路径中找到可执行文件。如果我再次运行“get it”命令,我可以看到路径被删除了。我需要做些什么来更新$env吗? - andrew lorien
我用 "ValueToRemove" 替换了 'ValueToRemove',现在一切正常。 - ZAY

-1

我想更新这篇文章。仅更改$Env:Path或使用[System.Environment] :: SetEnvironmentVariable()不会像Rich先前发布的帖子那样永久更改路径变量(也许自原始帖子以来Windows已经更改了)。它只会在PowerShell中的那个会话中更改它。一旦您退出并重新启动powershell,路径将恢复。为了在powershell中永久更改它,您必须更改注册表中的值。

以下是修改注册表的另一种方法(可以修改以适应新的所需路径)。

#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

这些信息也可以在下面的外部网站找到: https://codingbee.net/powershell/powershell-make-a-permanent-change-to-the-path-environment-variable


-2

克里斯的答案在重启后不会持久化。为了在重启后使其工作,您需要修改PATH的注册表位置。以下是一个函数示例,用于从路径中删除项目和添加项目:

# 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"

9
Chris的答案仍然持续存在,因为他在[System.Environment]::SetEnvironmentVariable()调用中使用Machine作为范围,这将在后台更新注册表(使用User也是如此,它会更新当前用户的定义;只有使用Process是非持久性的)。使用[System.Environment]::SetEnvironmentVariable()不仅比直接修改注册表更方便,还通过WM_SETTINGCHANGE消息通知其他应用程序进行持久更改。 - mklement0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接