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.
I think you're confusing privileges and user account if you type somthing like echo "%username%" in the console, displayed name will be "bob"
?? 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.
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.
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.
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
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" ?
Maybe Code: for /f "tokens=2* delims=:\" %%a in ('tasklist /fi "imagename eq explorer.exe" /fo list /v^|find "\"') do set "cuser=%%b"