I've been messing around with environment variables lately, and then some of them got duplicated, so I wrote a script to reorder them.
Environment variables have a length limit, if you exceed it, such as SqlServer-related, the common path will get a separate variable declaration, such as
The variable length can be significantly compressed by manually replacing its path with the following
But.The Powershell script reverts these back to the original paths when it gets the environment variables againSuggests clicking on theEdit Text
, copy it down and replace the script's$list
Variable values.
Okay, with the caveat out of the way and the script posted.It is recommended to back up and check before operation
# Direct access to variables,as if%JAVA_HOME%The original text will be displayed,May result in lengths greater than2047Leads to partial failure
$list = $env:Path -split []::NewLine
# Getting examples from a copy of the system interface
# $list="%JAVA_HOME%\bin
# C:\app\YiXinDa\product\21c\dbhomeXE\bin
# %ProgramFiles86%\Common Files\Oracle\Java\javapath
# %windir%\system32
# %windir%
# %windir%\System32\Wbem
# %windir%\System32\WindowsPowerShell\v1.0\
# %windir%\System32\OpenSSH\
# %ProgramFiles86%\Intel\Intel(R) Management Engine Components\DAL
# %ProgramFiles%\Intel\Intel(R) Management Engine Components\DAL
# %MSSQL86%\100\Tools\Binn\" -split []::NewLine
$uniqueArray = $list | Select-Object -Unique
$uniqueArray = $uniqueArray | Sort-Object
$final = New-Object ("")
foreach ($item in $uniqueArray) {
$($item + ";")
# Write-Host $item
}
Write-Host $final
[Environment]::SetEnvironmentVariable("PATH", $final , "Machine")