27

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.

2
  • 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
    Dec 19, 2018 at 9:54
  • 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
    Sep 24, 2019 at 12:17

5 Answers 5

29

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")
5
  • 5
    Thank you very much. Of all of these, the %NEWPATH% option is the most palatable, so I think I'll go with that. Dec 19, 2018 at 12:27
  • 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
    Jul 25, 2019 at 21:36
  • also see here: stackoverflow.com/questions/34491244/… Sep 24, 2019 at 7:25
  • I've used newpath several times it seems to be deleted in fact converted into variable and added. probably other programs adding their path Jan 23, 2020 at 2:04
  • You can also use symlinks to shorten paths. Example: mklink /J "C:\PF86" "C:\Program Files (x86)"
    – cowlinator
    Mar 6 at 2:04
8

I tried this. It is working for me after following @harrymc’s answer even if you are facing this issue try this.

Make a new path2 with all contents from path.

Now place %path2% inside path

enter image description here

enter image description here

So now %path2%; %path3%; %path4%; ... and so on.

6
  • It doesn't work.
    – chansey
    Dec 19, 2021 at 19:14
  • tried increasing using regedit ? Dec 20, 2021 at 20:58
  • I have not tried, because this answer said it doesn't work as well. I currently temporary solution is using short path (i.e. PROGRA~1, PROGRA~2).
    – chansey
    Dec 22, 2021 at 10:53
  • This is what I did, actually, and it worked fine. I don't see where the other answer says it doesn't work and it actually implies it does. I broke PATH up into groups, like _SYSTEM, _PROGRAM_FILES, _PROGRAM_FILES_X86, and so forth. Then PATH became %_SYSTEM%;%_PROGRAM_FILES%;%_PROGRAM_FILES_X86%; and so forth. This is a great way to organize and manage PATH, though you do have to watch for the correct order and conflicts as you would with PATH in any case.
    – Richard_G
    Oct 12, 2022 at 16:34
  • this is probably the most clean solution
    – liang
    Apr 12, 2023 at 9:15
3

While the dialog that you used limits the length that environment variables to 2047 characters, the registry editor does not have that limitation. The registry editor can be opened with the run dialog which can be opened with the Win+R keyboard shortcut. Typing regedit and pressing enter will open the registry editor. Environment variables are listed under Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ Control\Session Manager\Environment and Path can be found among them. Right-clicking Path will open a menu, of which Modify... can be selected to modify the value of the variable. As always, when working with the registry editor, make sure that you only change what you intend to, as some changes may have less than desirable side effects.

1
3

Create a new variable, path2, and place that variable in your path variable. That should work. Also, I'm using an app called PathCopy to shorten all my path names in my path. That is helping also.

I've also tested this:

$pathEnv = [System.Environment]::GetEnvironmentVariable('Path','Machine')

$splitPaths = $pathEnv.Split(";") 
$shortPaths = @()
ForEach ($path in $splitPaths) {
    $a = New-Object -ComObject Scripting.FileSystemObject
    
    # Try to get the short path
    try {
        $shortPath = $a.GetFolder("$path").ShortPath
        if (-not [String]::IsNullOrEmpty($shortPath)) {
            $shortPaths += $shortPath
        } else {
            # If short path is null or empty, use the original path
            $shortPaths += $path
        }
    } catch {
        # If an error occurs (e.g., folder does not exist), use the original path
        $shortPaths += $path
    }
}

$newPath = $shortPaths -join ";"

[System.Environment]::SetEnvironmentVariable('Path',$newPath,'Machine')

0
0
  1. create path2
  2. put %path2% to path
  3. check at cmd if work , as example python...

enter image description here

enter image description here

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .