I found plenty of questions like this one on StackExchange but no one work in my specific case.

I would like to easily add literally %FOO% to the user environment variable PATH.

I found the solution below. Unfortunately it doesn't work as expected.

for /f "skip=2 tokens=3*" %a in ('reg query HKCU\Environment /v PATH') do @if [%b]==[] ( @setx PATH "%~a;%FOO%" ) else ( @setx PATH "%~a %~b;%FOO%" )

It doesn't work if:

  • The user PATH variable doesn't exist
  • The user PATH exists and is empty
  • The user PATH is almost 255 char long.

Moreover it doesn't add the %FOO% literally but expands it.

Is there any possibility to easily do it?


It depends on what you're trying to do:

  • To update the PATH in your current commandline session only, use: set PATH=%PATH%;%FOO%.
  • To edit it for the current user only, use: setx PATH "%PATH%;%FOO%". Note that this change is not visible in your current command line session; you need to start a new command line.
  • To edit it for all users on the machine, use: setx /M PATH "%PATH%;%FOO%".

You can view the path by typing ECHO %PATH% in a command line or by checking it in the Windows environment settings.

Also, in Windows 7 and 8, the maximum environment variable string size is 32,767 characters. Although this is also valid for the PATH variable, a command in command console has a maximum length of 8191 characters, so you need to take that into consideration when using the PATH variable in command line commands. Anyhow, you have some more head-space above the 256 characters you have right now.

  • Good news for the max length! – nowox Jan 13 '15 at 11:54
  • The maximum accessible variable length within the command console (cmd.exe) is 8191 bytes (the maximum command line length). This is true from XP through Win 8. I haven't tested whether PATH values longer than 8191 bytes function properly, but I'm pretty sure not. So yes, most programs can work with variables up to 32k, but large values are no good for the command console. – dbenham Jan 13 '15 at 15:26
  • @dbenham: added a remark about that. – agtoever Jan 13 '15 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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