in windows I would like to have a cmd script that I can run which sets an environment variable. The variable should still be set after the script executes. The variable is a token of sorts and is greater than 1024 characters.

set command is scoped to the current script and gets its own copy of any environment variables. So once the script completes the global variable is not set.

I've tried setx but this truncates to 1024 characters and there doesn't seem to be a way around it - my token is more than 1024 characters.

This is NOT the PATH variable Im trying to set. Ive seen many posts to directly update the windows registry - but they seem to all be referencing explicitly to the PATH variable and the registry key for that specific variable. I need a general solution for any custom variables I have on my machine. My variable is something used to access a code repo and Im trying to automate setting it so I dont have to remember the steps every time I build code. I find it hard to believe there is no simple option to persist a variable from a command prompt without directly editing the registry. I guess if that is the only option and it works for all environment variables I would consider suggestions to use that as a last resort. Updating registry is also less favorable since I cant share this script with other developers who mostly are not system admins. But I can be selfish and just solve for myself since Im a system admin.

Windows variables when set directly in the GUI can be longer than 1024 characters. Below image is 2000 character variable.

enter image description here enter image description here

  • eh? I can set it directly in the environment modal window in windows so that seems not correct. Im using windows 10 BTW. Maybe your distinction is about system variables - Im talking about user variables.
    – George
    Dec 21, 2023 at 18:16
  • 1
    @Ramhound: That was true in MS-DOS era (or rather the entire environment block was limited to about 512B~4kB if I remember correctly), but not quite true for WinNT now; Microsoft says the limit of a single environment variable is 32kB. Dec 21, 2023 at 18:25
  • @u1686_grawity - Then I am completely confused by the question
    – Ramhound
    Dec 21, 2023 at 19:05
  • 1
    @George - There isn’t a fundamental difference between system variables and user variables.
    – Ramhound
    Dec 21, 2023 at 19:06
  • 1
    If using PowerShell is an option, you could store this in a variable that lives in your $PROFILE and reference it that way. You could also use PowerShell to set the system env var using [System.Environment]::SetEnvironmentVariable('MyVarName', $MyVarValue, 'Machine') -- I used $MyVarValue = [string]::new('X', 4000) for this test in Win10 and was able to open cmd.exe and echo %MyVarName% and it didn't truncate anything. FYI, the environment var limitation of 32,767 chars includes the var names + var values.
    – Paul π
    Dec 21, 2023 at 20:04

2 Answers 2


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 another tool to set long variable. The tool to use is PowerShell.

PowerShell calls Windows API directly and so can approach the theoretical limit of 32,767 characters for an environmental variable.

You may run a one-time command such as this to create your long variable, or you may even read the long string from a file:

[Environment]::SetEnvironmentVariable("Path", $longpath, "User")

For more information, see Microsoft's about_Environment_Variables.

  • thanks for the suggestion! I ended up updating by regex directly. Seems like your option would probably work as well and will try to find time to test it out as well and post back.
    – George
    Dec 23, 2023 at 5:47

Thank everyone for posting options! I ended up writing a script to update directly via regex then I call setx on a dummy variable which propagates the changes (without it the regex update doesnt update the variable till you log out and back in). Would be nice if setx worked for bigger values but what can we expect from microsoft :(

  • for /f "tokens=3 delims=^ " %%i in ('reg query HKCU\Environment ^| findstr /i c:"YourVariableName"') do echo/ %%i, the value in setx == output %%i
    – Io-oI
    Dec 26, 2023 at 18:40

You must log in to answer this question.

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