236

I have recently upgraded to Windows 10 from Windows 8.1.

Now I wanted to set an environment variable for my new installation of Apache Maven.

Each time I created the user variable, things were fine. However, I also need to create the system variable where I will need to append the bin directory to the variable that I already create in the user variable to be "path".

Now, each time I do this, I get an error that says "This environment variable is too large". As a result of this, I am unable to create the path.

I have attached an image of this error.

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
olasammy
  • 6,676
  • 4
  • 26
  • 32
  • 48
    I hope they fix this some day. This should never happen.^^ – itmuckel Jun 24 '16 at 08:22
  • 6
    The correct solution is for applications to stop misusing `PATH` environment variable. [The proper solution has existed for nearly 2 decades now](https://learn.microsoft.com/en-us/windows/win32/shell/app-registration). But getting developers to use it is like pulling teeth. – Ian Boyd Jul 06 '19 at 14:31
  • 1
    @IanBoyd - Putting the path in the `App Paths` registry area is something for the installer program to do. Not something most users are comfortable with. So you are correct in pointing your finger at us developers. :( – Jesse Chisholm Oct 10 '20 at 14:41
  • 4
    @IanBoyd Certainly! If you can convince the tool writers to live only in Windows, and never ever start their work in Linux, then migrate to Windows and OS/X. And at least two of the above listed file names comes from Microsoft. ;-) ;-) ;-) – Jesse Chisholm Oct 11 '20 at 18:29
  • SQL Server Management Studio seems to be particularly bad at filling up the PATH variable with long paths. One instance of SSMS will take up over 10% of the PATH variable by itself. – Simon Elms Jul 01 '21 at 22:58

15 Answers15

228

When the PATH environment variable gets overloaded with too many values it reaches a point where you cannot add values any more. Trying the following should solve your problem.

Solution 1:

  1. Create a new system environment variable, say 'NEWPATH'
  2. Assign the bin directory location to 'NEWPATH'
  3. Now append '; %NEWPATH%' to the PATH environment variable

If this still doesn't work then try to copy some part of the PATH environment variable already existing values to the 'NEWPATH' and then append the 'NEWPATH'.

Solution 2:

Check the value of the PATH environment variable if you can group and shorten the paths. For example,

C:\Program Files\Microsoft SQL Server\102\Tools\Binn;C:\Program Files\Microsoft SQL Server\102\DTS\Bin;

can be combined to

C:\Program Files\Microsoft SQL Server;

In this way, you can build more space into your fixed length PATH variable and finally adjust your bin directory location into PATH.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Swapnil Kamat
  • 2,305
  • 1
  • 8
  • 4
  • Yes you are very correct. So much research had showed me that path variable can only take 2048 characters and when the characters get to this stage, the path variable will not able to take much character anymore. So I tried what you suggested and I am now fine. Thank you – olasammy Dec 30 '15 at 03:59
  • Thank you! That's a great idea @ solution 1! Should've thought of that myself... :) – Dev-iL Jan 13 '17 at 20:44
  • 6
    This works like a charm, thank you! However, one has to be cautious when using `setx PATH` because this will directly resolve the `%NEWPATH%` and the resulting string will be once again too long... So, using this approach, only the dialog and not the command line should be utilized – IceFire Mar 29 '17 at 16:15
  • 3
    @SwapnilKamat This doesn't completely work : I am using miktex, its `pdflatex` has to be in `PATH`, if I put the location of `pdflatex` in `NEWPATH`, `pdflatex` it's not found anymore ("the command `pdflatex` is not recognized" ...) – Olórin Nov 18 '17 at 18:06
  • 28
    Ironically, Microsoft SQL Server set so many path variables that it alone nearly fills up the space. I created a PATHS_MSSQL variable and then added %PATHS_MSSQL% to the PATH variable to take their place. Freed up a lot of space in the PATH variable. – Steve Lloyd Aug 26 '18 at 03:55
  • 1
    Great tip. I named it Path2 to keep them together in a list. – Alexander Puchkov Jun 15 '20 at 15:26
  • 1
    Whoever looking for what @steveLloyd commented. This is it https://superuser.com/questions/902907/do-i-need-sql-server-paths-in-my-path-environment-variable-for-it-to-work – Binoy Sep 08 '20 at 06:16
  • This is honestly just hilarious. I really appreciate the combination of Env Vars – lifelonglearner Dec 17 '20 at 04:57
  • Worth noting that I had to restart my machine after creating NEWPATH. My computer wasn't recognising the paths I moved from PATH to NEWPATH until I restarted it. – Simon Elms Jul 02 '21 at 01:22
  • 1
    @SimonTewsi environment variables don't get re-read until you restart the terminal session – Cardin Jan 24 '22 at 02:30
  • I've been using this trick for ages, but the problem is that PATH still runs into the length limit. My PATH contains only a few entries of %NEWPATHS% and so I can still add more entries to PATH without a problem. Problem is that my %NEWPATHS% are already quite long and so even if I add a new entry to PATH, it doesn't recognize it. Checking $env:PATH in Powershell will show that the PATH gets cut off at a certain point. – shadowz1337 Aug 02 '23 at 04:18
95

There are a few ways to clean up your path variable. The easiest is to use Rapid Environment Editor. This free utility will,

  1. Remove the duplicate paths (right click → Cleanup Paths)
  2. Remove non-existent folders (shown in red which you need to manually delete)
  3. Replace long paths with short paths (right click → long to short path).

I do the above steps in order and use the third step only for the longest paths until the Path variable size is under control again.

If you want to go more advanced, here's a little C# tool that you can modify to whatever other logic you want to implement.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shital Shah
  • 63,284
  • 17
  • 238
  • 185
  • 5
    You have to run Rapid Environment Editor with administrator privileges. Otherwise these options will be disabled. – Tides Sep 27 '21 at 09:04
45

Another solution, or more a workaround to bypass the PATH environment variable length limit, is to manage your path (add, remove or update) using a PowerShell script;

  1. Capture the current PATH variable by clicking "Edit Text" (see above screenshot) and copy it to your clipboard and save it in a text file as a backup too to avoid bad surprises. This is not mandatory, but will allow you to recover should something go wrong.

  2. Now that it is backed up, add the following to a new PowerShell (.ps1) file (amending the first line below with the folder path(s) you want to add (the part after the + sign):

$newPath = $env:Path + '; C:\Users\....\FirstFolderToAddToPath; C:\Users\....\SecondFolderToAddToPath;'

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

$env:Path = $newPath

This is how I managed to get my (long) PATH variable back after playing with the Windows 10 UI, being caught by the length limitation and losing most of my path.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sifou13
  • 611
  • 6
  • 4
  • Thanks.. This was easy. Curious whats the difference between assigning value in line 2 and 3 – Nayak Sep 07 '17 at 15:47
  • Line 3 sets it in the current session, line 2 sets it as the machine default. – Tetsujin no Oni Sep 13 '17 at 22:06
  • 3
    PS works for longer path but traditional `setx /m` truncates to 1048 chars :(. You can also edit path in text editor and just do `[Environment]::SetEnvironmentVariable("Path", '\my\new;path1', "Machine")`. You will need admin shell. – Shital Shah Oct 03 '17 at 23:49
  • It's possible using PS but not using GUI. Hm, how does that make sense? Upvoted, thanks! – Legends Jun 29 '20 at 12:29
  • The script works however, it also appends the variables defined in the user variables Path (under my user account) to the system Path – Kunal May 06 '21 at 21:11
33

Try to modify by RegEdit. In my case it works when length is more than 3000.

Press Win + R and enter regedit to open RegEdit. Go to Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment, and modify the value of Path to your path. And restart the computer, and it should work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yu yang Jian
  • 6,680
  • 7
  • 55
  • 80
16

In addition to the answer of Swapnil, note that you can modify the maximum length of the Path environment variable - which is otherwise limited to 2048 characters (while a single path has an historical limit of 206 characters).

In Windows 10, you achieve this by setting the LongPathsEnabled registry key to 1, which can be found here:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem

To access the registry editor: Windows key + R, type Regedit.

Source: Windows 10 “Enable NTFS long paths policy” option missing

Also take a look at this Super User answer: An answer to Windows 10 “Enable NTFS long paths policy” option missing

Note that the error "Environment variable is too large" is related to the whole variable, not the single path currently being added (to answer the "inaccurate advice" comment below).

Additional note: app compatibility

The text in the registry key/group policy related to LongPathsEnabled reads:

Enabling NTFS long paths will allow manifested win32 applications and Windows Store applications to access paths beyond the normal 260 char limit per node. Enabling this setting will cause the long paths to be accessible within the process.`

The caveat here is the term manifested. In general applications need to declare capabilities explicitly in a manifest file; most win32 applications since the days of Windows Vista are manifested. To use long paths, the app manifest needs a longPathAware element:

<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
        <ws2:longPathAware>true</ws2:longPathAware>
    </windowsSettings>
</application>

More info here.

Some users complained that LongPathsEnabled is not yet supported by Windows Explorer and other apps, although in general working alternatives can be found. For example, in this forum post an user states that

The only explorer alternative that does support long paths and long file names is total commander. It also allows to edit file names and open/process the files IF the accepting application also uses the extended API function.
Notepad seems to. MKVtoolnix too. Also, very surprisingly, MPC-HC, even though it hasnt been in development for years. Not surprisingly, sucky VLC does not (seriously?) and neither does the lovely PotPlayer.

There is also an interesting SuperUser discussion on this. In particular, this answer describes two feasible alternatives to use long paths: using Cygwin (a *nix emulation layer) or splitting the long path using NTFS Junction Points (essentially a link, like a symbolic link).

alelom
  • 2,130
  • 3
  • 26
  • 38
  • 23
    Is this accurate advice? The maximum length of PATH variable (i.e. *concatenation of multiple paths*) is 2048 characters. 260 characters that you mentioned is the maximum length of *one path*. They're related but not the same. – Hendy Irawan Sep 22 '17 at 15:22
  • Yes it is accurate. I originally had mistakenly written that the length of the `Path` variable was 260, but I edited the post to correct it. The single path has a historical limit of 206, while the total length of the `Path` variable is 2048, and this latter limit can be modified as explained. – alelom Jan 05 '21 at 14:38
  • 1
    A word of caution. A comment against the second Super User answer says: _It's worth noting that most programs (Windows Explorer included) don't yet recognize the long file paths feature yet_ – Simon Elms Jul 01 '21 at 23:57
  • @SimonTewsi thanks, I've added a section to elaborate on that point. – alelom Aug 11 '21 at 08:37
11

I changed all the paths to variables for Program Files and programdata (this one saves like one character, though not as important).

For something like Node.js, I changed the normal path of

C:\Program Files\nodejs\

to

%ProgramFiles%\nodejs\

This can be done with "C:\Program Files (x86)" as well using "%ProgramFiles(x86)%".

It saved me a few characters, but enough that it stopped complaining, I feel.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bryan
  • 111
  • 1
  • 2
  • 9
    Why not just use %x86% to replace "C:\Program Files (x86)" and %x64% to replace "C:\Program Files", seems like that's a much better way to reduce character overhead. – Xorcist Apr 10 '17 at 21:07
  • 1
    @Xorcist - nice one! Had no idea about those – mark1234 Jul 26 '17 at 09:39
  • I don't think they exist by default but you can create them yourself. – Gabriel Fair Nov 24 '17 at 22:31
  • i also did like that, i created %PF% for "c:\Program FIles"and using it about 20 times allowed me to shorten the path enough. – UnDiUdin Feb 12 '20 at 09:40
  • 1
    @Xorcist how is that done by creating another variable? – Demodave May 04 '20 at 20:08
  • @Demodave, go to the "Environment Variables" window, click the `New...` button, and add `x86` for the name and `%ProgramFiles(x86)%` as the value; then add `x64` with `%ProgramFiles%` for the value. (note: the new variables won't be available in any command windows that are already opened.) – pbarney Oct 06 '21 at 15:58
8

I found you can do it via PowerShell.

[System.Environment]::SetEnvironmentVariable("PATH", "C:\Program Files (x86......etc.....", "Machine")

So I grabbed the existing system PATH, pasted into Notepad, added my new thing, and then pasted into the "C:\Program Files" bit of the above. Path was updated. Done.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

Apparently Rapid Environment Editor will do this for you (from Shital Shah's answer), but you can also shorten the paths to their 8.3 filename version. You will get a lot of mileage with just these four replacements:

C:\Program Files       --> C:\PROGRA~1
C:\Program Files (x86) --> C:\PROGRA~2
C:\Users\vd-wps\AppData\Roaming --> %APPDATA%
C:\Users\vd-wps\AppData\Local --> %LOCALAPPDATA%

If you copy your current path into Notepad, first search and replace C:\Program Files (x86) and then C:\Program Files.

Valerij Dobler
  • 1,848
  • 15
  • 25
bfris
  • 5,272
  • 1
  • 20
  • 37
  • 2
    I originally tried this approach, but my path slowly grew with duplicate entries as system & app updates added the paths back in using the non-8.3 paths (presumably after discovering that this string didn't exist in the path). The end result was a path even longer than when I started, and which needed manual cleanup. – Steve Hollasch May 11 '21 at 02:49
2

Workaround:

Please restart the system. After restarting the system, PATH is no longer empty, but it may get truncated to 2047 (4095) characters

If the system restart does not help, please:

Launch C:\windows\system32\regedit.exe. Go to the registry hive "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" and clean up unnecessary directories from the “Path” key. Restart the system.

Note: In some exceptional cases if the system is not able to start, please:

  • Login in the safe mode

  • Open the command prompt shell and type:

     reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d ^%SystemRoot^%\system32;^%SystemRoot^% /f
    

For more details:

Limitation to the length of the System PATH variable

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LeoZ
  • 635
  • 6
  • 5
  • How to increase the limit to at least `4095`? – Dan M. May 28 '18 at 14:23
  • 1
    WARNING! That last `reg add` statement will overwrite your existing path variables and they will be lost forever. It's better to first make a backup of them before running that command so that you can go back in and manually fix the problem. Here's how you can make a backup: `reg export "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" path.tmp /y & type path.tmp|find "Path"|find "C:\\Windows" > path.txt & del path.tmp` Your old path will be in `path.txt` – pbarney Oct 06 '21 at 15:35
  • 1
    The original article at intel.com is no longer available. Here's the archived version: [Limitation to the length of the System PATH variable](https://web.archive.org/web/20190519191717/https://software.intel.com/en-us/articles/limitation-to-the-length-of-the-system-path-variable). – Bass Jan 17 '22 at 09:48
2

In addition to other methods (e.g., PowerShell), I found a nice GUI, "Rapid Environment Editor" that can handle larger text values.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amir
  • 1,722
  • 22
  • 20
1

You can also try going through your variables to see if there are any irrelevant paths you could delete. This would free up some space for you to add another or more variables.

1

So I figured out the same problem I had, I noticed there were many duplicates pointing to the same location. I removed the duplicates which can be done with the delete option when you go click the "edit Environment Varibles" button.

You could instead edit text, copy the text from there, remove duplicates using any popular apps like notepad, excel (use the delimiter as ; then remove duplicates), or use python (use .split(";"), convert into a set, ";".join(stringSet), copy into a notepad file, then replace \ with \ using the ctrl+ H ie find and replace).

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jith Z
  • 11
  • 1
0

I had exactly the same problem. Eventually I had to delete one of the existing variables to make the total length less than 2047.

Then I could add %MAVEN_HOME%\bin to the path variables.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
one-hand-octopus
  • 2,229
  • 1
  • 17
  • 51
0

I found this AutoHotkey script useful, for editing or adding to my extremely long path (3743 chars now):

https://gist.github.com/roblogic/59898966f3ce594eb78a5b6147013194

(I'm not sure what the consequence of such a long path is; I may still have to fix it.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaron West
  • 187
  • 2
  • 5
-1

Workaround: Use the Edit text button and edit your PATH in a text editor.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christoph Adamakis
  • 865
  • 2
  • 9
  • 26