Search code examples
bashwindows-7cygwinadministratorrunas

How to get the actual current user in Cygwin when "run as administrator"?


Basically the cygwin CLI (or bash in particular, if that helps) equivalent of this question. I want to start cygwin as administrator, but then get my name, not the administrator's name, somehow. (All the environment variables like $USER, $USERNAME, etc. get set to the administrator.)

This could include any utility that comes with Windows, but it has to come with all editions, not just Pro.


Solution

  • realCurrentUser=$(wmic PATH Win32_Process WHERE "Name='explorer.exe' AND SessionId=1" CALL GetOwner | grep User | sed 's/^\s*User = "//' | sed 's/";$//')