Environment Variable inconsistency


GreenRaven

New member
Local time
11:10 PM
Posts
3
OS
Windows 11
We utilize the ClientName variable in our production environment, but we've had some inconsistent results in it's behavior.

From what I understand, when using remoting technology such as RDP, windows creates a value in HKCU\Volatile Environment\<session ID> called 'CLIENTNAME' with the value being the remote device's computer name. Then, when software is launched, it reads from HKCU\Volatile Environment, HKCU\Environment, and HKLM\System\CurrentControlSet\Control\Session Manager\Environment.

Herein lies my dilemma. Occasionally, when our users log into their sessions (we utilize Omnissa Horizon VDI, using Horizon Blast) for the day, some of them (roughly 1-2%) will not have the Environment variable set for CLIENTNAME. This is validated by the 'set' command being ran in command prompt, with the results not showing the CLIENTNAME variable shown at all. When I check the registry, the value exists and is populated with the client device name. I then re-launch CMD and run the command again, to validate it is not a timing issue, and it still does not show.

Then I restart explorer.exe using task manager. Once I do this, and re-launch CMD again, it shows when I run the set command. I could see maybe explorer caches some variables and it's referenced from there, but that thought falls apart when I continue testing.

Once the variable is set and explorer is relaunched, I can make any changes to the registry key/value and it will reflect upon re-launching CMD, as expected.
There are other variables in HKCU\Volatile Environment\<session ID> that also do not populate until explorer.exe is restarted.

Is this a bug or am I misunderstanding how windows handles environment variables?

Thank you.
 
Windows Build/Version
25H2

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP

My Computers

System One System Two

  • OS
    Windows 11 Pro 25H2 26200.8655
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 7080
    CPU
    i9-10900 10 core 20 threads
    Motherboard
    DELL 0J37VM
    Memory
    32 gb
    Graphics Card(s)
    none-Intel UHD Graphics 630
    Sound Card
    Integrated Realtek
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    2x1tb Solidigm m.2 nvme /External drives 512gb Samsung m.2 sata+2tb Kingston m2.nvme
    PSU
    500w
    Case
    MT
    Cooling
    Dell Premium
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    so slow I'm too embarrassed to tell
    Browser
    #1 Edge #2 Firefox
    Antivirus
    Defender+MWB Premium
  • Operating System
    Windows 11 Pro 24H2 26200.8457
    Computer type
    PC/Desktop
    Manufacturer/Model
    Beelink Mini PC SER5
    CPU
    AMD Ryzen 7 6800U
    Memory
    32 gb
    Graphics card(s)
    integrated
    Sound Card
    integrated
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    1TB Crucial nvme
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    still too embarrassed to tell
    Browser
    Firefox
    Antivirus
    Defender
    Other Info
    System 3 is non compliant Dell 9020 i7-4770/24gb ram Win11 PRO 26200.8457
Is this a bug or am I misunderstanding how windows handles environment variables?

Slight misunderstanding. This part isn't true...

Then, when software is launched, it reads from HKCU\Volatile Environment, HKCU\Environment, and HKLM\System\CurrentControlSet\Control\Session Manager\Environment.

A process gets an environment block at creation time, and that block is usually inherited from the parent process. The registry locations you mention are primarily used to construct an initial environment block during logon/session initialization. After that, processes mostly inherit from already-running parent processes.

In your case, probably either explorer was started before the volatile environment variables were merged, or before Horizon finished populating everything.

Any process launched from explorer (cmd, Run dialog, shortcuts, etc.) would inherit explorer's process block. That's why the variable is present after you restart explorer.

As for why registry changes are reflected later, well it's possible cmd could re-query the volatile environment, or Windows or some APIs may do it, but I wouldn't rely on that. The initial shell process for the user's session is what matters the most.

But yeah it is frustrating.... we had to do stuff like this in my Citrix days...

Powershell:
    if (Test-RegistryValue -Path "Registry::HKLM\Software\Citrix\Roles\XenApp" -Name "Installed")
    {
        [string]$XenAppInstalled = (Get-ItemProperty -Path "Registry::HKLM\Software\Citrix\Roles\XenApp").Installed
        if ($XenAppInstalled -eq "1")
        { # XenApp is installed. Wait for the ClientName environment variable to be set.
            Write-Log "XenApp is installed."
            $maxSleepSeconds = 45
            Write-Log "Waiting for up to $($maxSleepSeconds) seconds for CLIENTNAME environment variable to populate."
            $clientName = [System.Environment]::GetEnvironmentVariable("CLIENTNAME", [System.EnvironmentVariableTarget]::User)
            while (([string]::IsNullOrEmpty($clientName)) -and ($maxSleepSeconds -gt 0))
            {
                Sleep -Seconds 1
                $maxSleepSeconds--
            }
            Write-Log "Finished waiting for CLIENTNAME environment variable to populate."
            Write-Log "CLIENTNAME variable = `"$clientName`""
        }
    }
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
A process gets an environment block at creation time, and that block is usually inherited from the parent process. The registry locations you mention are primarily used to construct an initial environment block during logon/session initialization. After that, processes mostly inherit from already-running parent processes.
This does make sense with the initial behavior, but why does it behave differently after explorer is re-launched? I'm asking so I can have a better idea of how windows works, since it's not adding up for me still.

Thank you @pseymour. I have a feeling I'm going to implement something similar to that script, I just have to figure out what layer will allow me to integrate it.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP
That's what I was saying about maybe cmd refreshing some variables, or Windows, or some API... I don't know the internals of that, so anything I say would be a guess. I do know that the PowerShell code I posted reads the environment variable, not the registry, and it eventually gets the CLIENTNAME value, so it's possible for a given process to get an up-to-date environment variable at some point, but I wouldn't know what that is for any given process. Maybe in the case of Citrix XenApp back then, maybe it (XenApp) was forcing a broadcast message of some sort saying, "hey I updated the environment variables, everyone check back in!" I dunno.

There are other smart folks around; maybe someone knows.
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
Without knowing too much about your specific environment, but there is a chance that it happens because the users, when leaving the remote session, just close the remote desktop instead of logging off their session. It can also happen when you use the thing locally.

Just closing the remote window leaves everything running in the server, including explorer, any cmd window and all applications, each one retaining their respective environments. Since environment variables are populated at process start, you may be left with an older set (this is why everyone tells you to reboot when changing them). If you logoff instead of just closing, your programs are closed and when the next session starts you get a fresh set of environment variables.

Another way to tackle this is to forget entirely about environment variables (as you've seen how unreliable they are) and just use the Windows API directly to get this value. Have a look here for example:
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
@Alejandro85, as much as I would love it, the software has to point at an environment variable. If it's not the native one, I would essentially have to capture the data and put it into an environment variable anyway.
The issue does not come from a stale session. As of right now there is a completely separate issue we're facing where any disconnect completely locks users out of their sessions (It's on the backburner and I haven't had a chance to look at it too closely yet)

I appreciate the info though. If it's more of a constant, I may do that process instead. I'll have to do some testing.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP
I am no expert, but in terms of what I could find......Windows doesn’t reread the environment from the registry every time you start a program. Each process keeps its own copy of the environment it was given when it started, so if explorer has an old environment, apps launched from explorer will inherit that old copy.

When a process starts another process without a custom environment, the child gets a copy of the parent’s environment. From what I can find, Microsoft’s help pages say the new process uses the calling process environment. That means the environment is a snapshot, not a live view of the registry. So when you open explorer it will only show the environment explorer had at the time you launched it.

I would guess it is more likely a timing issue between horizon agent writing the info and explorer initializing. I know you said you ruled it out, but explorer might be too busy tripping over itself during log on for a second, or maybe you have some logon scripts or something delaying it, or maybe it is because win 11 has a mind of its own.

I would check the following:
  1. Horizon agent and client versions, are they up to date? Maybe you have some on sportatic versions? Are you on current release (CR) or the extended service branch (ESB) ? I would imagine the extended service branch would be ideal for testing this.

  2. Do you have any logon optimizations, scripts, GPOs etc that might start slow down explorer at login? Try disabling them as a test and see if things improve.

This might be helpful:

 

My Computers

System One System Two

  • OS
    Windows 11 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Custom Built
    CPU
    Ryzen 7 5700 X3D
    Motherboard
    MSI MPG B550 GAMING PLUS
    Memory
    64 GB DDR4 3600mhz Gskill Ripjaws V
    Graphics Card(s)
    RTX 4070 Super , 12GB VRAM Asus EVO Overclock
    Monitor(s) Displays
    Gigabyte M27Q (rev. 2.0) 2560 x 1440 @ 170hz HDR
    Hard Drives
    2TB Samsung nvme ssd
    4TB Western Digital nvme ssd
    PSU
    CORSAIR RMx SHIFT Series™ RM750x 80 PLUS Gold Fully Modular ATX Power Supply
    Case
    CORSAIR 3500X ARGB Mid-Tower ATX PC Case – Black
    Cooling
    ID-COOLING FROSTFLOW X 240 CPU Water Cooler
    Keyboard
    Logitech G213
    Mouse
    Logitech G203
    Internet Speed
    1.2gbps Fiber 😎
  • Operating System
    Chrome OS
    Computer type
    Laptop
    Manufacturer/Model
    HP Chromebook
    CPU
    Intel Pentium Quad Core
    Memory
    4GB LPDDR4
    Monitor(s) Displays
    14 Inch HD SVA anti glare micro edge display
    Hard Drives
    64 GB emmc
The login script thing is something I thought about yesterday, but in the opposite way of what Andrew is thinking. Windows doesn't wait for logon scripts, by default, before loading the shell. There are GPOs to set it to wait, but the default is off. I can't recall their names, but it's something like "wait for logon scripts" and "wait for the network." I was wondering yesterday if you set those one or both of those policies to wait, if you would have more consistent results. Neither one has anything to do with environment variables, per se, but maybe the delay itself is what the doctor ordered.
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
A child process inherits the parent process's environment variables. Now if you're in an interactive shell session or running a script, that parent process can update its own variables.

But if your shell is File Explorer, then it doesn't have any way to update its environment. There isn't an API call for that. So you would have to kill Explorer, and hope the parent process in your session creates a new Explorer which inherits the variables you wanted. But if the parent process wasn't refreshed with CLIENTNAME, then you're in the same place as before.
 

My Computer

System One

  • OS
    Windows 7
From that link to the knowledge base I posted previously I just noticed it did not fill in the title making it look like a generic link

In some versions of Win11 VDI, environment variables related to Horizon Client may not be retrieved immediately after logon. (6001139)

Anyway it states the following:

  • When a user connects to a vdi, Omnissa Horizon Client gathers information about the client system and the connection broker sends that information to the remote desktop.
  • Horizon Agent writes the client computer information to the system registry path HKCU\Volatile Environment on remote desktops that are deployed on single-user machines.
  • This value is stored as a VDI environment variable with the following format: ViewClient_***
  • For example, it can be referred to using the set command in the command prompt.
  • However, in some versions of Win11 VDI, environment variables related to Horizon Client may not be retrieved immediately after logon.
  • This may be occurred due to the time lag when retrieving that value after VDI logon.

The following workarounds are suggested.

  • After logon to a VDI, wait approximately 1–2 minutes before launching the command prompt for referring the value.
  • Use the reg query "HKCU\Volatile Environment" to retrieve values from the registry.
  • In the Command Prompt, open a new tab and reference the value there.
 

My Computers

System One System Two

  • OS
    Windows 11 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Custom Built
    CPU
    Ryzen 7 5700 X3D
    Motherboard
    MSI MPG B550 GAMING PLUS
    Memory
    64 GB DDR4 3600mhz Gskill Ripjaws V
    Graphics Card(s)
    RTX 4070 Super , 12GB VRAM Asus EVO Overclock
    Monitor(s) Displays
    Gigabyte M27Q (rev. 2.0) 2560 x 1440 @ 170hz HDR
    Hard Drives
    2TB Samsung nvme ssd
    4TB Western Digital nvme ssd
    PSU
    CORSAIR RMx SHIFT Series™ RM750x 80 PLUS Gold Fully Modular ATX Power Supply
    Case
    CORSAIR 3500X ARGB Mid-Tower ATX PC Case – Black
    Cooling
    ID-COOLING FROSTFLOW X 240 CPU Water Cooler
    Keyboard
    Logitech G213
    Mouse
    Logitech G203
    Internet Speed
    1.2gbps Fiber 😎
  • Operating System
    Chrome OS
    Computer type
    Laptop
    Manufacturer/Model
    HP Chromebook
    CPU
    Intel Pentium Quad Core
    Memory
    4GB LPDDR4
    Monitor(s) Displays
    14 Inch HD SVA anti glare micro edge display
    Hard Drives
    64 GB emmc

Latest Support Threads

Back
Top Bottom