系统环境变量中有多个“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()
小心并检查此链接寻找兼容性。