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 ?

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
  • 498,455
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