How do I keep each PATH entry only once?
Is there a way to get rid of the duplicates without manual editing?
Preferably without installing third-party software?
If you don't mind using a PowerShell script then you can remove duplicates using the following script from the Microsoft Script Center:
Script to check for duplicate paths in PATH environment variable
Sometimes repeated installation of software can add duplicate entries into the PATH environment variable. Since environment variable has a there is a hard coded limit in the size of this variable, there are chances that you may it that limit over a period of time. This script checks the PATH environment variable and removes any duplicate path entries.
$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()
Source How to check for duplicate paths in PATH environment variable