1

I have a very long string I am trying to store in a Machine level Environment Variable.

Set-Item -Path Env:\... seems to work, but I dont see a way to set a machine level environment variable with this, only local variables.

[Environment]::SetEnvironmentVariable(..., ..., "Machine")

Has a 4095 character limit and truncates.

$Enviro = ([Microsoft.Win32.Registry]::LocalMachine).OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $True)
$Enviro.SetValue(..., ..., [Microsoft.Win32.RegistryValueKind]::ExpandString);
$Enviro.Close()

Also seems to have a similar limit and truncates the string.

$Enviro = "registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\"
Set-ItemProperty -Path $Enviro -Name ... -Value ...

Also, once again, same issue. Truncates to 4095.

What other options do I have?

To people that keep linking the "Environment variables not working properly" article, that doesnt answer this question. That article covers reading from the environment/registery, not writing to it. This is the exact opposite use case. Please go over my code above and you will notice I tried Set-ItemProperty and it does not work.

0 Answers0