How do I bypass restrictions on the length of the PATH variable

10

3

error message

System Properties

This environment variable is too large. This dialog allows setting values up to 2047 characters long.

I use/have used a lot of software, and my PATH has grown as a result. When I try to add new paths, I get the above message. As a workaround, I'm editing my user path, but prefer not to.

Tobi Alafin

Posted 2018-12-19T09:48:20.480

Reputation: 221

In future posts, please add the text of the error message, which I assume you already searched for to confirm the question was not asked earlier ;-) Often, just hitting Ctrl+C in such dialog will copy it for you.

– Arjan – 2018-12-19T09:54:50.130

Perhaps ask on software recommendations for a utility to detect duplicate entries and entries where the directory does not exist (uninstallers are lousy about updating the path). This would be trivial to code as a Python script.

– Mawg says reinstate Monica – 2019-09-24T12:17:10.500

Answers

10

Microsoft's documentation says that an environment variable on Windows is limited to only 32,767 characters (link), but does not say how to create such a long variable.

The problem here is that the tools that Windows provides all have their limits :

  • The set and setx commands truncate values to 1023 characters.

  • Setting directly in the registry at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment fails since regedit truncates entered strings after 2047 characters.

So you must use workarounds.

Use short folder names

You may see such names by using dir /x /ad. The following example shows that on my computer the folder Program Files (x86) may be replaced by PROGRA~2:

enter image description here

Use embedded environmental variables

If you have:

C:\this_is_a\long_path\that_appears\in_multiple_places\subdir1
C:\this_is_a\long_path\that_appears\in_multiple_places\subdir2

then you can create a new environment variable such as:

SET P1=C:\this_is_a\long_path\that_appears\in_multiple_places

after which your original paths become

%P1%\subdir1
%P1%\subdir2

You may also split PATH into two by creating a new variable, say NEWPATH, containing the excess paths and append ;%NEWPATH% to the PATH variable.

Avoid using the setx command because it will directly resolve embedded environmental variables and the resulting string will be once again too long.

Use a PowerShell script to set the PATH

PowerShell calls Windows API directly and so can approach the theoretical limit of 32,767 characters for an environmental variable.

The script may contain commands such as:

[Environment]::SetEnvironmentVariable("Path", $longpath, "Machine")

harrymc

Posted 2018-12-19T09:48:20.480

Reputation: 306 093

3Thank you very much. Of all of these, the %NEWPATH% option is the most palatable, so I think I'll go with that. – Tobi Alafin – 2018-12-19T12:27:24.243

Splitting into multiple env vars, such as %NEWPATH%, also allows me to nest other env vars in %NEWPATH% (string expansion I guess happens at each nested level - have not tested to see what happens in an infinite reference loop..) – Coruscate5 – 2019-07-25T21:36:26.463

I've used newpath several times it seems to be deleted in fact converted into variable and added. probably other programs adding their path – Dreaded semicolon – 2020-01-23T02:04:09.413