Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to set environment variables using the setx command, such as follows

setx 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.

share|improve this question

4 Answers

up vote 7 down vote accepted

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.

share|improve this answer
Is reboot really required here ? How does setx do it ? Setx does not require reboot. – Madhur Ahuja Feb 9 '12 at 14:10
Yes, a reboot is required. setx edits the registry as I indicated, then broadcasts a WM_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 replace SETX that makes a registry edit followed by the broadcast), but just like setx and the System Properties envvar dialog, it has side-effects that make rebooting preferable. – Synetech Feb 9 '12 at 19:05
Can you share your program with me ? I will be very useful. – Madhur Ahuja Feb 10 '12 at 3:08
I 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 Feb 10 '12 at 3:28
Oh 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”. :-| – Synetech Feb 10 '12 at 3:40
show 3 more comments

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...

share|improve this answer
> 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. – Synetech Jun 28 '12 at 5:39

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('path2', "$($newPath);$($oldPath)",'Machine');

The Environment.SetEnvironmentVariable() API call will broadcast WM_SETTINGCHANGE so you do not need to reboot.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.