RunAs Administrator returning "SYSTEM" as $env:USERNAME in PowerShell #20701

Open
opened 2026-01-31 07:21:38 +00:00 by claunia · 16 comments
Owner

Originally created by @mpearon on GitHub (Oct 19, 2023).

Originally assigned to: @lhecker on GitHub.

Windows Terminal version

1.19.2682.0

Windows build number

10.0.22621.0

Other Software

Windows PowerShell 5.1.22621.2428
PowerShell 7.3.8

Steps to reproduce

Launch Terminal as Administrator (Win+X, A)
Issue the following:

$env:USERNAME

Expected Behavior

The username of the currently logged in user should be returned

Actual Behavior

SYSTEM is returned

The screenshot included shows PWSH 7.3.8 running outside Terminal (top left) and inside Terminal (top right) and Windows PowerShell 5.1 running outside Terminal (bottom left) and inside Terminal (bottom right).
image

The output of $env:USERNAME (which is the expected output) has been redacted in the screenshots and the prompt customized to prevent data leak - but this occurs with no customizations and with a fresh installation of Terminal from the Windows Store.

Originally created by @mpearon on GitHub (Oct 19, 2023). Originally assigned to: @lhecker on GitHub. ### Windows Terminal version 1.19.2682.0 ### Windows build number 10.0.22621.0 ### Other Software Windows PowerShell 5.1.22621.2428 PowerShell 7.3.8 ### Steps to reproduce Launch Terminal as Administrator (Win+X, A) Issue the following: ```powershell $env:USERNAME ``` ### Expected Behavior The username of the currently logged in user should be returned ### Actual Behavior `SYSTEM` is returned The screenshot included shows PWSH 7.3.8 running outside Terminal (top left) and inside Terminal (top right) and Windows PowerShell 5.1 running outside Terminal (bottom left) and inside Terminal (bottom right). ![image](https://github.com/microsoft/terminal/assets/7157790/5ef3961f-12c0-48d0-ba64-bdc4f7114657) The output of `$env:USERNAME` (which is the expected output) has been redacted in the screenshots and the prompt customized to prevent data leak - but this occurs with no customizations and with a fresh installation of Terminal from the Windows Store.
claunia added the Issue-BugProduct-TerminalPriority-2Area-TerminalConnection labels 2026-01-31 07:21:39 +00:00
Author
Owner

@mpearon commented on GitHub (Oct 19, 2023):

I learned something about the $env: automatic variables.
I just assumed they were immutable, but they are not. I would like to see this change in behavior explained, but going forward, I guess I will use [System.Environment]::UserName, because that seems a lot safer.

@mpearon commented on GitHub (Oct 19, 2023): I learned something about the `$env:` automatic variables. I just assumed they were immutable, but they are not. I would like to see this change in behavior explained, but going forward, I guess I will use `[System.Environment]::UserName`, because that seems a lot safer.
Author
Owner

@DHowett commented on GitHub (Oct 19, 2023):

Do you use "over-the-shoulder" elevation (where you need to provide an admin username and password which is different from your main user)?

@DHowett commented on GitHub (Oct 19, 2023): Do you use "over-the-shoulder" elevation (where you need to provide an admin username and password which is different from your main user)?
Author
Owner

@237dmitry commented on GitHub (Oct 19, 2023):

I could not reproduce:

adm

@237dmitry commented on GitHub (Oct 19, 2023): I could not reproduce: ![adm](https://github.com/microsoft/terminal/assets/78153320/1ca1dfcd-82cb-40d0-9ea7-ef31ffe8068d)
Author
Owner

@mpearon commented on GitHub (Oct 19, 2023):

Do you use "over-the-shoulder" elevation (where you need to provide an admin username and password which is different from your main user)?

@DHowett - I'm leveraging Win+X, A - which triggers UAC. At that prompt, I'm providing a credential that is different than my logged in user.

I could not reproduce:

adm

@237dmitry - Terminal version 1.19.2682.0? Leave it to me to have the weird issue.

@mpearon commented on GitHub (Oct 19, 2023): > Do you use "over-the-shoulder" elevation (where you need to provide an admin username and password which is different from your main user)? @DHowett - I'm leveraging Win+X, A - which triggers UAC. At that prompt, I'm providing a credential that is different than my logged in user. > I could not reproduce: > > ![adm](https://user-images.githubusercontent.com/78153320/276723569-1ca1dfcd-82cb-40d0-9ea7-ef31ffe8068d.png) @237dmitry - Terminal version 1.19.2682.0? Leave it to me to have the weird issue.
Author
Owner

@DHowett commented on GitHub (Oct 19, 2023):

I'm providing a credential that is different than my logged in user.

So, I think it might be because of this. "Over-the-shoulder" elevation is slightly different from the normal split-token kind of elevation that most folks use.

If you launch PowerShell directly as admin, without terminal (Win+R, pwsh, Ctrl+Shift+Enter), do you see the same thing?

@DHowett commented on GitHub (Oct 19, 2023): > I'm providing a credential that is different than my logged in user. So, I think it might be because of this. "Over-the-shoulder" elevation is slightly different from the normal split-token kind of elevation that most folks use. If you launch PowerShell directly as admin, without terminal (Win+R, `pwsh`, <kbd>Ctrl+Shift+Enter</kbd>), do you see the same thing?
Author
Owner

@237dmitry commented on GitHub (Oct 19, 2023):

Terminal version 1.19.2682.0

No, I updated WT to 1.19.2831.0 with msixbundle from release page, but I do not remember such behavior.

@237dmitry commented on GitHub (Oct 19, 2023): > Terminal version 1.19.2682.0 No, I updated WT to 1.19.2831.0 with msixbundle from release page, but I do not remember such behavior.
Author
Owner

@mpearon commented on GitHub (Oct 19, 2023):

I'm providing a credential that is different than my logged in user.

So, I think it might be because of this. "Over-the-shoulder" elevation is slightly different from the normal split-token kind of elevation that most folks use.

If you launch PowerShell directly as admin, without terminal (Win+R, pwsh, Ctrl+Shift+Enter), do you see the same thing?

@DHowett - No. This is what my screenshot is demonstrating (left side is launching PowerShell outside of Terminal). This issue only occurs when Terminal is in play.

@mpearon commented on GitHub (Oct 19, 2023): > > I'm providing a credential that is different than my logged in user. > > So, I think it might be because of this. "Over-the-shoulder" elevation is slightly different from the normal split-token kind of elevation that most folks use. > > If you launch PowerShell directly as admin, without terminal (Win+R, `pwsh`, Ctrl+Shift+Enter), do you see the same thing? @DHowett - No. This is what my screenshot is demonstrating (left side is launching PowerShell outside of Terminal). This issue only occurs when Terminal is in play.
Author
Owner

@DHowett commented on GitHub (Oct 19, 2023):

Whoops, I'm sorry about that. Shows how well I pay attention.

This may be a side effect of us regenerating the user environment block from first principles rather than inheriting it from Explorer. Hmm.

@DHowett commented on GitHub (Oct 19, 2023): Whoops, I'm sorry about that. Shows how well I pay attention. This may be a side effect of us regenerating the user environment block from first principles rather than inheriting it from Explorer. _Hmm._
Author
Owner

@mpearon commented on GitHub (Oct 19, 2023):

No worries! I'm sure you see a million of these issues a day.
This behavior breaks a few scripts that use the automatic variables, but it did lead me to "discover" that these are not immutable and really shouldn't be trusted for what I was using them for, so I have switched to using [System.Environment] - but I'm still very curious.

I'm happy to do whatever testing is required.

@mpearon commented on GitHub (Oct 19, 2023): No worries! I'm sure you see a million of these issues a day. This behavior breaks a few scripts that use the automatic variables, but it did lead me to "discover" that these are not immutable and really shouldn't be trusted for what I was using them for, so I have switched to using `[System.Environment]` - but I'm still very curious. I'm happy to do whatever testing is required.
Author
Owner

@lhecker commented on GitHub (Oct 24, 2023):

Note to self: I probably accidentally fixed this in #16190 and I just need to fix the incorrect comment here:
64b5b2884a/src/inc/til/env.h (L435-L437)

@lhecker commented on GitHub (Oct 24, 2023): Note to self: I probably accidentally fixed this in #16190 and I just need to fix the incorrect comment here: https://github.com/microsoft/terminal/blob/64b5b2884a3ef4dbed5026c73079f4ffcdad6de5/src/inc/til/env.h#L435-L437
Author
Owner

@ghostbuster1002 commented on GitHub (Feb 28, 2024):

I probably accidentally fixed this in https://github.com/microsoft/terminal/pull/16190

@lhecker
FWIW, I am still getting $env:UserName as SYSTEM when trying this on v1.19.3172.0

@ghostbuster1002 commented on GitHub (Feb 28, 2024): > I probably accidentally fixed this in https://github.com/microsoft/terminal/pull/16190 @lhecker FWIW, I am still getting $env:UserName as SYSTEM when trying this on [v1.19.3172.0](https://github.com/microsoft/terminal/releases/tag/v1.19.3172.0)
Author
Owner

@GitUser200607 commented on GitHub (May 17, 2024):

I had the same issue when Windows Terminal with PowerShell was elevated via UAC, and to evade it, I changed $Env:USERNAME to [Environment]::UserName

@GitUser200607 commented on GitHub (May 17, 2024): I had the same issue when Windows Terminal with PowerShell was elevated via UAC, and to evade it, I changed `$Env:USERNAME` to `[Environment]::UserName`
Author
Owner

@eryksun commented on GitHub (May 18, 2024):

The regenerate() function initially sets the value of USERNAME from the current environment, via get(til::details::vars::user_name). Then the value gets overwritten by the system environment's USERNAME value of "SYSTEM", due to get_vars_from_registry(HKEY_LOCAL_MACHINE, til::details::vars::reg::system_env_var_root).

regenerate() depends on setting the right value again from the "Volatile Environment" (non-persistent) registry key that gets created for a normal logon session, from get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_volatile_env_var_root). The problem is that OTS elevation doesn't implement a normal logon that creates the "Volatile Environment" registry key. I think OTS elevation should do a full logon, if it can, but Terminal shouldn't depend on that. It should set USERNAME again from the current environment after loading the system environment from the registry.

4a243f0445/src/inc/til/env.h (L445-L475)

Also, some variables from a normal logon session are missing in the OTS case, such as LOGONSERVER, HOMEDRIVE, HOMEPATH, and HOMESHARE. Maybe Terminal should manually set HOMEDRIVE and HOMEPATH based on USERPROFILE if they don't exist in the current environment.

@eryksun commented on GitHub (May 18, 2024): The `regenerate()` function initially sets the value of `USERNAME` from the current environment, via `get(til::details::vars::user_name)`. Then the value gets overwritten by the system environment's `USERNAME` value of "SYSTEM", due to `get_vars_from_registry(HKEY_LOCAL_MACHINE, til::details::vars::reg::system_env_var_root)`. `regenerate()` depends on setting the right value again from the "Volatile Environment" (non-persistent) registry key that gets created for a normal logon session, from `get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_volatile_env_var_root)`. The problem is that OTS elevation doesn't implement a normal logon that creates the "Volatile Environment" registry key. I think OTS elevation should do a full logon, if it can, but Terminal shouldn't depend on that. It should set `USERNAME` again from the current environment after loading the system environment from the registry. https://github.com/microsoft/terminal/blob/4a243f044572146e18e0051badb1b5b3f3c28ac8/src/inc/til/env.h#L445-L475 Also, some variables from a normal logon session are missing in the OTS case, such as `LOGONSERVER`, `HOMEDRIVE`, `HOMEPATH`, and `HOMESHARE`. Maybe Terminal should manually set `HOMEDRIVE` and `HOMEPATH` based on `USERPROFILE` if they don't exist in the current environment.
Author
Owner

@42sol-eu commented on GitHub (Jul 9, 2024):

We have a somewhat connected issue (I let you judge if this belongs here if not I will open a separate issue).

System

Or computers have Local Admin users (not the default Administrator).
We can spawn a admin-shell via runas.exe /user:admin{username}

The fault

Let us assume the useris muspet and the domain ourdomain
If using the runas /user:adminmuspet@ourdomain %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe as a start shell.

Enter the password for adminmuspet@ourdomain:
Attempting to start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe as user "adminmuspet@ourdomain" ...
RUNAS ERROR: Unable to run - C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
1326: The user name or password is incorrect.

[process exited with code 1 (0x00000001)]
You can now close this terminal with Ctrl+D, or press Enter to restart.

Expected Behaviour

  • It should show the correct failure (because password and user are definitively correct)
  • It should work properly (start the system powershell)

I am human

  • Maybe I am missing something OR our IT is doing something strange.
@42sol-eu commented on GitHub (Jul 9, 2024): We have a somewhat connected issue (I let you judge if this belongs here if not I will open a separate issue). ## System Or computers have `Local Admin` users (not the default Administrator). We can spawn a admin-shell via `runas.exe /user:admin{username}` ## The fault Let us assume the useris `muspet` and the domain `ourdomain` If using the `runas /user:adminmuspet@ourdomain %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe` as a start shell. ``` Enter the password for adminmuspet@ourdomain: Attempting to start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe as user "adminmuspet@ourdomain" ... RUNAS ERROR: Unable to run - C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 1326: The user name or password is incorrect. [process exited with code 1 (0x00000001)] You can now close this terminal with Ctrl+D, or press Enter to restart. ``` ## Expected Behaviour * It should show the correct failure (because password and user are definitively correct) * It should work properly (start the system powershell) ## I am human * Maybe I am missing something OR our IT is doing something strange.
Author
Owner

@42sol-eu commented on GitHub (Nov 12, 2024):

@zadjii-msft is this still open? Is it planned to be fixed in the near future?

@42sol-eu commented on GitHub (Nov 12, 2024): @zadjii-msft is this still open? Is it planned to be fixed in the near future?
Author
Owner

@zadjii-msft commented on GitHub (Nov 12, 2024):

Yep, I bet this is still broken. I certainly don't have time to fix this in the near future, sorry 😕

@zadjii-msft commented on GitHub (Nov 12, 2024): Yep, I bet this is still broken. I certainly don't have time to fix this in the near future, sorry 😕
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20701