REG_EXPAND_SZ environment variables not properly expanded for shells #13344

Open
opened 2026-01-31 03:40:15 +00:00 by claunia · 0 comments
Owner

Originally created by @chrisdjali-wrld3d on GitHub (Apr 7, 2021).

Windows Terminal version (or Windows build number)

1.7.572.0

Other Software

No response

Steps to reproduce

  1. Set a system environment variable including %USERPROFILE%, e.g. TestVariable set to %USERPROFILE%\bin.
  2. In Regedit, confirm that the type is REG_EXPAND_SZ by finding it under HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.
  3. Start PowerShell or CMD normally, e.g. from the WinX menu, or Shift+RMB in Windows Explorer.
  4. Print the variable's value, e.g. echo $env:TestVariable.
  5. Observe that %USERPROFILE% has been expanded, e.g. C:\Users\Chris\bin is printed.
  6. Open Windows Terminal and create a new PowerShell or CMD tab.
  7. Print the variable's value, e.g. echo $env:TestVariable.
  8. Observe that %USERPROFILE% has not been expanded, e.g. %USERPROFILE%\bin is printed.

Expected Behavior

Like with standalone PowerShell or CMD via CONHOST, the environment variable is properly expanded.

Actual Behavior

The unexpanded value is used.

This isn't happening with every environment variable - others get expanded correctly, like windir being %SystemRoot% becoming C:\WINDOWS. My suspicion is that it's just variables not defined in the system scope not being expanded if they're used in the system scope.

I'm pretty sure this is Windows Terminal doing something weird rather than Windows itself as when viewing the WindowsTerminal.exe process' environment in Process Explorer, the variables are properly expanded, but when doing the same for the actual shell process, they aren't. If I had to guess, I'd say maybe Windows Terminal is attempting to read the up-to-date environment from the registry when starting new shells instead of letting them inherit its potentially outdated environment, but is doing so inconsistently with how Windows does it. If that's actually what's happening, it may be sensible to use Chocolatey's RefreshEnv batch script or PowerShell module as a reference implementation as that gets the same result as Windows.

This is happening in MSYS-esque Bash shells, too, but the Windows-to-Unix path conversion seems to be doing another round of expansion that masks the symptoms.

Originally created by @chrisdjali-wrld3d on GitHub (Apr 7, 2021). ### Windows Terminal version (or Windows build number) 1.7.572.0 ### Other Software _No response_ ### Steps to reproduce 1. Set a system environment variable including `%USERPROFILE%`, e.g. `TestVariable` set to `%USERPROFILE%\bin`. 2. In Regedit, confirm that the type is `REG_EXPAND_SZ` by finding it under `HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment`. 3. Start PowerShell or CMD normally, e.g. from the WinX menu, or Shift+RMB in Windows Explorer. 4. Print the variable's value, e.g. `echo $env:TestVariable`. 5. Observe that `%USERPROFILE%` has been expanded, e.g. `C:\Users\Chris\bin` is printed. 6. Open Windows Terminal and create a new PowerShell or CMD tab. 7. Print the variable's value, e.g. `echo $env:TestVariable`. 8. Observe that `%USERPROFILE%` has not been expanded, e.g. `%USERPROFILE%\bin` is printed. ### Expected Behavior Like with standalone PowerShell or CMD via CONHOST, the environment variable is properly expanded. ### Actual Behavior The unexpanded value is used. This isn't happening with *every* environment variable - others get expanded correctly, like `windir` being `%SystemRoot%` becoming `C:\WINDOWS`. My suspicion is that it's just variables not defined in the system scope not being expanded if they're used in the system scope. I'm pretty sure this is Windows Terminal doing something weird rather than Windows itself as when viewing the `WindowsTerminal.exe` process' environment in Process Explorer, the variables are properly expanded, but when doing the same for the actual shell process, they aren't. If I had to guess, I'd say maybe Windows Terminal is attempting to read the up-to-date environment from the registry when starting new shells instead of letting them inherit its potentially outdated environment, but is doing so inconsistently with how Windows does it. If that's actually what's happening, it may be sensible to use Chocolatey's RefreshEnv batch script or PowerShell module as a reference implementation as that gets the same result as Windows. This is happening in MSYS-esque Bash shells, too, but the Windows-to-Unix path conversion seems to be doing another round of expansion that masks the symptoms.
claunia added the Issue-TaskNeeds-Tag-FixProduct-TerminalArea-TerminalConnection labels 2026-01-31 03:40:15 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#13344