![删除路径系统变量中重复的环境变量是否安全?](https://linux22.com/image/1613009/%E5%88%A0%E9%99%A4%E8%B7%AF%E5%BE%84%E7%B3%BB%E7%BB%9F%E5%8F%98%E9%87%8F%E4%B8%AD%E9%87%8D%E5%A4%8D%E7%9A%84%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E6%98%AF%E5%90%A6%E5%AE%89%E5%85%A8%EF%BC%9F.png)
系统环境变量中有多个“C:\WINDOWS”环境变量和其他各种重复的环境变量,由于我的环境变量太大,我想删除它们。
答案1
您可以使用 Powershell 脚本删除 PATH 环境变量中的重复路径。在此之前,您应该导出环境变量。转到regedit
并按照路径导出值: 来源
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKCU\Environment
接下来尝试这个 Powershell 脚本:
$RegKey = ([Microsoft.Win32.Registry]::LocalMachine).OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $True)
$PathValue = $RegKey.GetValue("Path", $Null, "DoNotExpandEnvironmentNames")
Write-host "Original path :" + $PathValue
$PathValues = $PathValue.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
$IsDuplicate = $False
$NewValues = @()
ForEach ($Value in $PathValues)
{
if ($NewValues -notcontains $Value)
{
$NewValues += $Value
}
else
{
$IsDuplicate = $True
}
}
if ($IsDuplicate)
{
$NewValue = $NewValues -join ";"
$RegKey.SetValue("Path", $NewValue, [Microsoft.Win32.RegistryValueKind]::ExpandString)
Write-Host "Duplicate PATH entry found and new PATH built removing all duplicates. New Path :" + $NewValue
}
else
{
Write-Host "No Duplicate PATH entries found. The PATH will remain the same."
}
$RegKey.Close()
小心并检查此链接寻找兼容性。