0

I'm currently searching for a way to overcome the limit off the environment variable length on Window OS, and the maximum limit I have achieved is 4133. But for my project, I need to check the system installation with longer path environment (8000, 16000 or even the maximum 32000).

setx command is out, since it automatically truncated the path environment to 1024 character.

I have tried the API SetEnvironmentVariable() of PowerShell and it seems to have limit of ~4133 character, the rest is automatically truncated or not function properly.

Is there anyway to overcome this limit ?

  • Were you trying the win32 API or some .NET API? The former is documented as having a maximum of 32k, so truncation would be odd. – u1686_grawity Oct 22 '18 at 05:32
  • All environment variables (names, values, delimiters, terminator) occupies one memory block. The size of this block is 32kb (-1 byte). Your task looks like unsolvable... without extending this block at least (and I don't know how to do it). – Akina Oct 22 '18 at 06:05
  • I don't think it's possible to do that. But it's not necessary to use `%PATH%` for verification anyway. Why don't just use an absolute path, or just CD into the folder? I'm sure there are a lot of redundant paths in the variable. And even if the length is still too long you can easily mount the path to a shorter one with `subst`, `mountvol` or a symbolic link. [Overcoming the 1024 character limit with setx](https://superuser.com/q/387619/241386) – phuclv Oct 22 '18 at 06:05
  • @grawity that is for user-defined variables. The limit for `%PATH%` variable is [2047/4095 characters](https://software.intel.com/en-us/articles/limitation-to-the-length-of-the-system-path-variable), https://support.microsoft.com/en-us/help/2685893/error-messages-after-you-change-the-2047-character-limit-in-an-environ, https://blogs.msdn.microsoft.com/oldnewthing/20100203-00/?p=15083 – phuclv Oct 22 '18 at 06:05

2 Answers2

1

Microsoft's documentation says that an environment variable on Windows is only limited to 32,767 characters (link). It does not say how to create a very 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.

As far as I can see, your only remaining option is to write a small program using the Windows API function SetEnvironmentVariable, whose documentation specifies the limit of 32,767 characters. Scripts would not help here, since they also have their limits.

harrymc
  • 455,459
  • 31
  • 526
  • 924
0

I did a minor modification to the script from https://gallery.technet.microsoft.com/scriptcenter/How-to-check-for-duplicate-5d9dd711

and works fine for my Win 10 long path

$RegKey = ([Microsoft.Win32.Registry]::LocalMachine).OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $True)
$PathValue = $RegKey.GetValue("Path", $Null, "DoNotExpandEnvironmentNames")
Write-host "Original path :" + $PathValue 


$NewValue = $PathValue + ";C:\Program Files\Microsoft\ML Server\PYTHON_SERVER;C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\Library\bin;C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\Library\scripts"
$RegKey.SetValue("Path", $NewValue, [Microsoft.Win32.RegistryValueKind]::ExpandString)
Write-Host "New Path :" + $NewValue


$RegKey.Close()
todorp
  • 1
  • 1