Getting current logged-in user while running as Admin

Discussion in 'Scripting' started by sacarias, Oct 30, 2019.

  1. sacarias

    sacarias MDL Junior Member

    Nov 21, 2018
    65
    1
    0
    So there are 2 users in local PC: "bob" (unprivileged; member of group Users) and "foo" (privileged; member of both Users and Administrators).

    I log in with "bob", right-click CMD and "Run as Administrator". So I'm running the entire CMD console as user "foo".
    How can I get into a variable the user "bob" from here, so I could run some commands such as "runas"?

    Thanks.
     
  2. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    348
    577
    10
    I think you're confusing privileges and user account
    if you type somthing like echo "%username%" in the console, displayed name will be "bob"
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. sacarias

    sacarias MDL Junior Member

    Nov 21, 2018
    65
    1
    0
    ??
    Yes, if I run CMD or batch script *normally*, %username% will be "bob".

    But if I run CMD or batch script through right-click and "Run as Administrator", CMD or script will be running as the only possible administrator user in this case: "foo". Don't know what if there are multiple admins, haven't tried that yet.
    So in this case, as proof, %username% will be necessarily "foo", since this user is member of the Administrators group.

    So, coming from logged-in user "bob" and now running CMD as user "foo", how can I get into a variable the user "bob" from here?

    Thanks again.
     
  4. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    348
    577
    10
    If you have multiple admins account it's the exact same, you have to choose which admin account to use and enter its password.

    So why not add "bob" temporary to the administrators group instead of entering "foo" password?
    net localgroup Administrators bob /add
    then once you're done
    net localgroup Administrators bob /delete
    To swich between admin users you can use something like that
    runas /user:bob /savecred "powershell -c start-process cmd.exe -verb runas

    What you want to do exactly ? would be more simple, "user bob into variable" means nothing, or anything to me...can be C:\Users\bob, could be bob SID etc.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. sacarias

    sacarias MDL Junior Member

    Nov 21, 2018
    65
    1
    0
    I need the "bob" user to remain standard user, not in the Administrators group.
    I need to log in with "bob", run CMD as Admin so CMD is running as "foo", and from this admin CMD session somehow get "bob" back in %username%.
    Assuming I don't know what bob's username is beforehand.
     
  6. RazTK

    RazTK MDL Novice

    May 26, 2012
    2
    4
    0
    I'm using: for /f "tokens=3*" %%a in ('tasklist /fi "imagename eq explorer.exe" /fo list /v^|find "\"') do if "%%b"=="" (set cuser=%%a) else set cuser=%%a %%b
     
  7. sacarias

    sacarias MDL Junior Member

    Nov 21, 2018
    65
    1
    0
    Does this work on all Windows 7, 8.1 and 10?

    Doesn't this result in %%a="username:" and %%b="<hostname\bob>" , thus cuser="username: hostname\bob" ?
     
  8. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    10,750
    44,713
    340
    Maybe
    Code:
    for /f "tokens=2* delims=:\" %%a in ('tasklist /fi "imagename eq explorer.exe" /fo list /v^|find "\"') do set "cuser=%%b"