Overcoming the 1024 character limit with setx
Solution 1
Your best bet is to edit the registry directly.
Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
and edit the Path
value (then reboot to activate the new value).
Note however that while you can enter a very long path, (up to the maximum environment variable length; 2,048 or 32,768 bytes depending on the source), not all software will be able to read and handle it correctly if it is too long.
Solution 2
if you are using windows vista or higher, you can make a symbolic link
to the folder. for example:
mklink /d C:\pf "C:\Program Files"
mklink /d C:\pf86 "C:\Program Files (x86)"
would make a link so c:\pf
would be your program files folder. I shaved off 300 characters from my path by using this trick.
(I know it's not related to setx but it is useful for people which are searching overcomming on 1024 char limit)
Solution 3
You could use a PowerShell script similar to the following:
$newPath = 'F:\common tools\git\bin;F:\common tools\python\app;F:\common tools\python\app\scripts;F:\common tools\ruby\bin;F:\masm32\bin;F:\Borland\BCC55\Bin'
$oldPath = [Environment]::GetEnvironmentVariable('PATH', 'Machine');
[Environment]::SetEnvironmentVariable('PATH', "$newPath;$oldPath",'Machine');
The Environment.SetEnvironmentVariable()
API call will broadcast WM_SETTINGCHANGE
so you do not need to reboot.
Solution 4
This open-source SetEnv command-line tool is good to edit the PATH and other environment variables without limitations. It uses a dynamic buffer so no static limitations like 1024.
http://www.codeproject.com/Articles/12153/SetEnv
The choice of a % as a prefix to append to a variable could have been better though, as makes the syntax difficult sometimes if used with other batch local variables...
Solution 5
A far superior tool than setx for path manipulation is pathed.exe. Unfortunately, it's limited to editing the path.
In addition to a superior user experience than setx, you don't have a 1024 character limit. Unlike direct registry manipulation, this application uses the Environment.SetEnvironmentVariable() API call which will broadcast WM_SETTINGCHANGE
.
Related videos on Youtube
Madhur Ahuja
“The best thing about a boolean is even if you are wrong, you are only off by a bit.” (Anonymous)
Updated on September 18, 2022Comments
-
Madhur Ahuja almost 2 years
I am trying to set environment variables using the
setx
command, such as followssetx PATH "f:\common tools\git\bin;f:\common tools\python\app;f:\common tools\python\app\scripts;f:\common tools\ruby\bin;f:\masm32\bin;F:\Borland\BCC55\Bin;%PATH%"
However, I get the following error if the value is more then 1024 characters long:
WARNING: The data being saved is truncated to 1024 characters.
SUCCESS: Specified value was saved.
But some of the paths in the end are not saved in variable, I guess due to character limit as the error suggests.
-
John Alexiou over 7 yearsLook into Rapid Environment Editor which allows you to edit all envirnoment variables graphically (you can also save a backup also).
-
Chad Schouggins almost 7 yearsIs anyone else bother by this claiming success when it clearly does not do what was requested? Is it not troubling that this doesn't fail while leaving the path exactly how it was?
-
cowlinator almost 7 yearsDoes this still truncate on windows 10?
-
Ivan over 6 yearsYes, it does on Windows 10
-
dkocich about 4 yearsit still does on windows 1909
-
Beliaev Maksim over 3 yearsFor windows I can recommend a module to set variables through registry in Python. Also has CLI app: github.com/beliaev-maksim/py_setenv this has good control on user/system level and does not have limit in length as setx
-
domih over 3 yearssetx is the worst as it destroys your applications which depend on path, so use the powershell one-liner described in a comment by @mark-c below like so:
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "[Environment]::SetEnvironmentVariable('path',\"%newPath%;$([Environment]::GetEnvironmentVariable('path','Machine'))\",'Machine');"
-
-
Madhur Ahuja over 12 yearsIs reboot really required here ? How does setx do it ? Setx does not require reboot.
-
Synetech over 12 yearsYes, a reboot is required.
setx
edits the registry as I indicated, then broadcasts aWM_SETTINGCHANGE
message. This tells all top-level windows that a system setting has changed (in this case an environment variable). Without that, Explorer and programs you opened with it will not know about the change. You could broadcast the message yourself manually (I wrote a program to do just that, and a batch file to replaceSETX
that makes a registry edit followed by the broadcast), but just likesetx
and the System Properties envvar dialog, it has side-effects that make rebooting preferable. -
Madhur Ahuja over 12 yearsCan you share your program with me ? I will be very useful.
-
Synetech over 12 yearsI suppose, but like I said, it won’t help. It has the same effects that
setx
and the Env-Var dialog do. It notifies the system that the vars. have changed, but they don’t actually take effect until you restart them. In other words, you have to exit and re-run every program that you want to know about the change. Is there a reason you can’t reboot? Can the longer path wait until you can? -
Synetech over 12 yearsOh and editing an env.var. actually causes problems if you don’t reboot because any environment variables that contain other variables will stop being expanded. For example, I just broadcast it and now this is my path and I get an error because the path is now “broken”/empty. None of the vars will expand properly again until I reboot. Unfortunately this is “normal”.
:-|
-
Justin Dearing about 12 yearsI found a small PowerShell script that will broadcast the WM_SETTINGCHANGE message.
-
Synetech about 12 years> SetEnv tool is good to edit the PATH and other environment variables without limitations Indeed. In fact, I had personally worked with Darka on that page to help make SetEnv even better so that it can fully support expanding/sub-variables (the Dynamic Variable Expansion section), which actually helps to reduce the length of the raw path.
-
Art almost 12 yearsYou can use the above registry method via the REG add command and then use SETX to set some other variable to itself (ie: SETX /M USERNAME %USERNAME%). This will cause the WM_SETTINGCHANGE message to be set and give your the ability to set the path from a batch file.
-
Fedor Alexander Steeman almost 11 yearsDon't you also need the following line: [Environment]::SetEnvironmentVariable('path', "$newPath",'Machine');
-
Ehtesh Choudhury over 10 yearsThere's a list of other tools (including pathed.exe) at superuser.com/questions/297947/is-there-a-convenient-way-to-edit-path-in-windows-7
-
G-Man Says 'Reinstate Monica' over 9 yearsCan you explain a bit more about what you are suggesting here?
-
Fantastory over 9 yearsIt is just a ready to use command to workaround setx 1024 character limit. Example is shown directly on PATH variable. Although the previous answers are helpful they lack a final solution.
-
twasbrillig over 9 yearsIn the 3rd line, I'm not sure what benefit the extra dollar signs and parentheses bring. Couldn't you just say
[Environment]::SetEnvironmentVariable('path', "$newPath;$oldPath",'Machine')
? -
Justin Dearing over 9 years@twasbrillig I'm just careful when I'm including a variable in a string. in this case its unecessary.
-
Andrew Morton over 8 yearsBe careful if you have used
fsutil.exe behavior set disable8dot3 1
. -
Andrew Steitz over 8 years@AndrewMorton good point but wouldn't that be "handled" by the fact that the script doesn't "generate" the 8.3 name, it just reports the 8.3 name that the file system assigned? So if a folder did not have an 8.3 name, the script would not propose a substitute. However, using
fsutil 8dot3name strip
AFTER using the script would definitely cause problems for affected folders. -
Andrew Morton over 8 yearsUnfortunately I don't have a spare drive or system to check it on. However, having used some
fsutil 8dot3name strip
earlier today and considering that (a) it checks the registry first and (b) the path is stored in the registry, my earlier concern may be unwarranted, as long as the user does not use the/f
(force) option. -
Braden Best almost 8 yearsWhat about the dialog box that allows you to set
%PATH%
? Editing the registry seems a bit extreme. -
Shital Shah almost 8 yearsFor current user, use HKEY_CURRENT_USER\Environment
-
Mark C almost 7 yearsYou can call this from an Administrator Command Prompt:
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -Command "[Environment]::SetEnvironmentVariable('path',\"C:\Program Files (x86)\GNU\GnuPG;$([Environment]::GetEnvironmentVariable('path','Machine'))\",'Machine');"
-
Ivan over 6 yearsLink is now broken
-
Eugene Petrov about 6 yearsWARNING! This possibly will break things, it will write
%path%;c:\...[snip]...\7z
to the userpath
variable, dropping anything else you had there. -
AnnanFay over 5 yearsThe PATH environment variable used by programs is a combination of the machine and local PATH variables. In addition to destroying the current data in the local path I don't think this does anything. Having
%path%
in the local path environment variable should have no effect. -
Smart Manoj about 5 years
-
Ekrem Dinçel over 4 years@JustinDearing the link is broken, may you update it?
-
Synetech over 4 years@EkremDİNÇEL, it's archived in the Wayback Machine.
-
Admin almost 4 yearsNo reboot required. I did this and restarting the command prompt window, all variables are initialized. No reboot.
-
Synetech almost 4 years@BosnianCoder, it depends on how you start the command-prompt. If you start it from Explorer, then it will/*should* have the new data. But if you run it from another program like if you have Task Manager running in the tray, or in my case, I use AutoHotkey to open a command prompt using a hotkey, then it won't work until you re-run that program (for some reason, either broadcasting
WM_SETTINGCHANGE
isn't working, or setx just doesn't do it right 🤷). Either way, saying to reboot is a blanket step to cover all scenarios. -
Amit Naidu over 3 yearsUseful when you are an administrator. For the rest of us:
You do not have sufficient privilege to perform this operation
. In a pinch,subst
can do something similar. -
Amit Naidu over 3 yearsSystem path and user path are separate for a reason. This combines them and puts everything in the system path. That is a major change to the environment behavior and is not expected by a user who is just looking to add something to their user path variable.