Duplicates in windows environment path
Asked Answered
M

3

5

For some reason I do not know, my echo %path% has many duplicates of C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\. As far as I know, long %path% is bad because it slows the searching process. Is it safe to remove these duplicates?

I also noticed that there are two version of path variable: one for user variables and one for system variables. If I type echo %path% in command prompt as normal user, it will show the concatenation of these two version (system version comes first). If I am to remove the duplicates, from which version should I remove?

(bold one is system version)

C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\MinGW\bin;C:\Python27;C:\Users\jie\AppData\Local\Microsoft\WindowsApps

Mellissamellitz answered 7/9, 2016 at 10:30 Comment(4)
Wonky installers are notorious for destroying the PATH system variable. Intel in particular is a ravenous name dropper and abusing the hell out of PATH. Just fix it with Control Panel > System > Advanced > Environment variables.Lamaism
Run => %windir%\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariablesDescry
Related: superuser.com/questions/1223976/… (How to remove PATH duplicates)Curvy
On my Win11 which is upgraded from Win10, I noticed that some OS paths are included twice in the system path (advanced system properties, environment variables): once 'plain', for example "C:\Windows\System32\Wbem", and once as "%systemroot%\system32\wbem". There were 4 or 5 of those, including %systemroot% = C:\Windows and %systemRoot%\system32 = C:\Windows\System32. All of them appeared twice in %PATH% in the environment.Keratoplasty
I
1

The paths you mention are system paths. They should stay in the PATH variable in the system scope. You can remove the duplicates in the PATH variable of the user scope, but you should reboot and check, if every application is still working (not because you deleted a duplicate, but to make sure you didn't delete something wrong by mistake). As usual, backup your PATH variables somewhere, before you start.

Duplicates inside each scope can always be safely removed. The list is split at every semicolon and each resulting path in the list is searched. If there are duplicates, the same search simply executes twice in the worst case. In the best case, the system might recognize the duplicates (I'm not sure if this happens), but this would mean additional effort for recognizing. So your statement on slowing down is correct in any case.

The reason for you duplicates (if it wasn't you at least) might probably be some application you installed somewhen, which edited the PATH variable improperly.

Incisure answered 7/9, 2016 at 10:59 Comment(3)
Thanks! Now my PATH is only 1/3 of the original length. The search feels faster now (or maybe it is just placebo effect?)Mellissamellitz
you're welcome. as long as it didn't get slower or break something, i'd consider it as ok.Incisure
I also had two duplicate values and it turned out that removing them from the user environment helped. But I wrote a little C# program checking Machine, User and Process environments. In Process and User I found duplicate semicolons ;; - but in Windows, when I checked System and User enviroment, everything looks okay. Then I saw there is %PyCharm% in the user enviroment which resolves in C# to C:\Program Files\JetBrains\PyCharm 2022.2.1\bin;;. Watch out, there may be other variables like this containing a semicolon already.Charioteer
P
4

On Microsoft Technet (Scriptcenter) there used to be a small PowerShellScript which checks against duplicate paths: How to check for duplicate paths in PATH environment variable

(It's gone now, at the original location, but archive.org has a copy.)

Should be run in a PowerShell environment with admin rights. I do this after every uninstall of any software (Windows 10 x64).

Peccant answered 16/12, 2017 at 10:55 Comment(0)
H
2

I was still wondering why some processes have a duplicate part in the PATH environment variable. Some investigating, the problem occurs by processes started by explorer.exe. It does not occur for processes which were not initially a child process (or 'child process of child process') of explorer.exe.

I think, the user part of the Windows PATH environment variable is influencend by :

  1. The two registry entries:
    • HKCU\Environment\Path
    • HKCU\Environment\1\Path

The process explorer.exe (which is runnng in the user context) loads both entries for the PATH environment variable. I deleted for the test HKCU\Environment\Path when HKCU\Environment\1\Path exists. The duplicate part of the PATH environment variable no longer exists.

Deleting or modifying this registry key should be done very carefully. Probably you ran in other unexpected problems, because the Windows Path environment variable affects all new processes started by the current user.

Note

Microsoft encourages the usage of App Paths, because the more entries the path environment variable Path has, the more time Windows is spending for searching for specified files. With App Paths you can specify per executable name the folders where the executable should search.

More information: Microsoft Docs about App Paths.

Note: A process which is started by cmd.exe or a .cmd or .bat does not look at the App Path key, even though if you use start process.exe the process.exe file will have a look at App Path\Process.exe\Path.

Happily answered 17/9, 2021 at 16:8 Comment(4)
Interesting, this answer helps me understand why there are entries I did not expect to find !Narcho
Follow up question: How to start Code such that it is not a descendant of explorer.exe?Raster
@Jann Poppinga: It seems this question is a little bit off-topic. Try the next .vbs script: Set objShell = CreateObject("Wscript.Shell") objShell.Run """C:\WINDOWS\system32\ping.exe"" 127.0.0.1 -t" It will create a process which is not child process of explorer.exe, I think. When you use Sysinternals' Process Explorer, you see it has no parent process ID.Happily
Oh, sorry, I hadn't realized that this question is about duplicate entries in general and not about the VS Code issue where it complains about these entries and doesn't debug a test. Anyway, I could start an orphan VS Code this way, but the issue persists. Thanks for your effort though.Raster
I
1

The paths you mention are system paths. They should stay in the PATH variable in the system scope. You can remove the duplicates in the PATH variable of the user scope, but you should reboot and check, if every application is still working (not because you deleted a duplicate, but to make sure you didn't delete something wrong by mistake). As usual, backup your PATH variables somewhere, before you start.

Duplicates inside each scope can always be safely removed. The list is split at every semicolon and each resulting path in the list is searched. If there are duplicates, the same search simply executes twice in the worst case. In the best case, the system might recognize the duplicates (I'm not sure if this happens), but this would mean additional effort for recognizing. So your statement on slowing down is correct in any case.

The reason for you duplicates (if it wasn't you at least) might probably be some application you installed somewhen, which edited the PATH variable improperly.

Incisure answered 7/9, 2016 at 10:59 Comment(3)
Thanks! Now my PATH is only 1/3 of the original length. The search feels faster now (or maybe it is just placebo effect?)Mellissamellitz
you're welcome. as long as it didn't get slower or break something, i'd consider it as ok.Incisure
I also had two duplicate values and it turned out that removing them from the user environment helped. But I wrote a little C# program checking Machine, User and Process environments. In Process and User I found duplicate semicolons ;; - but in Windows, when I checked System and User enviroment, everything looks okay. Then I saw there is %PyCharm% in the user enviroment which resolves in C# to C:\Program Files\JetBrains\PyCharm 2022.2.1\bin;;. Watch out, there may be other variables like this containing a semicolon already.Charioteer

© 2022 - 2024 — McMap. All rights reserved.