Forum Discussion

Schulzi's avatar
Schulzi
Brass Contributor
Sep 28, 2020

$env:Username Different Outputs

Hello,

 

I have a little Question/Problem using Powershell...

 

I got 2 Different PC running Windows-Powershell as Admin (both with same Domain-Account (example: cat40050))

BUT i get 2 Different Outputs...

On PC1 I get the current User that is logged on the System(example: T40050)

On PC2 I get the Admin shown that is running Windows-PS...(example: cat40050)

I noticed that one PS-Window is running as "user" and one in "system32"

Both startet from "C:\Windows\System32\WindowsPowerShell\v1.0\"

Why is it like that?

And is there a way to say "only use current logged on user" or start in/as C:\Users\T40...?

T4.... Users are normal Users with no Adminprivileges

cat4... Users are our local Admins (via Domain)

 

 

Greetings 

Yannik Schulz

  • farismalaeb's avatar
    farismalaeb
    Steel Contributor

    Schulzi 

    Hi

    $Env:Username is a dynamic variable set when the user login, you can also check the value for this variable from CMD by typing %Username%

    What might come to my mind is there is an impersonation when, such as a process runas, or Powershell executed under another credential

     

    • Schulzi's avatar
      Schulzi
      Brass Contributor

      Hi farismalaeb

       

      I just noticed that myself...

      I startet 2 Windows of PS now!

      1.  startet via "WIn+R -> pwsh -> CTRL+SHIFT+Return(Starts it as admin)"

       (cat is our local Domain Admin)

       

      2. startet via Startmenu (Powershell -> Run as Administrator)

      Startet in "System32"

       

       

      So why does Powershell start with different credentials?

      And how can I "say" which credential to choose?

       

      Greetings

      Yannik

       

       

       

      • farismalaeb's avatar
        farismalaeb
        Steel Contributor

        Schulzi 

        This is normal behavior, as PowerShell which "Run as Administrator" will start in the system32 directory, as it already has the access to it "it ran as admin"

        but running PowerShell normally will make PowerShell set the path to the user home directory as its the path it has access to, and it doesn't have full control over the system32 folder.

        the username used is the same

  • Schulzi's avatar
    Schulzi
    Brass Contributor

    Quick Update what I want to do:

    Launching Powershell as an Admin but still stay in the Current User (T4....-User) and not as the Admin User (cat....)

    The "T-Users" dont have Admin-privileges the "Cat-Users" have...

    BUT in need to stay logged on as the "T-User" in the new PS-Session/Windows that my script will work properly to edit all information in the "T-User-Profile".

     


    Greetings
    Yannik Schulz

Resources