how to determine if a shell is running as Administrator?

Andrew Schulman schulman.andrew@epa.gov
Thu Feb 5 09:44:00 GMT 2015


What's a reliable and efficient way to determine programmatically if the shell
that's running has elevated privileges?

Or if you prefer, how can I tell if the shell was started with "Run as
administrator"?

I'm looking for a solution that's reliable across installations and OS versions.
I want it also to be fairly fast, so it can be used for example in setting shell
prompts.

There's a SuperUser thread[1] that talks about this.  None of the solutions
seems completely satisfactory:

1. Run a command that only an Administrator can run, and check the return
status.  But which one?  Suggestions are:

  a. at. But at may not be on the $PATH, or supported.

  b. touch /cygdrive/c/Users/.x , or some other protected path.  But which paths
are protected is installation-dependent (as is the /cygdrive prefix).

  c. editrights -l.  On my host my unprivileged account can't even run this, so
I get a return status of 1.  In a privileged shell I get 0 or 2.  Is that
reliable?

  d. ??

2. Parse the output of groups or id -G.  I can't find any reliable way to do
this.  For example on my host, when I start a shell with "Run as administrator",
the new group I get isn't 544 (Administrators).  It's 114 (Local account and
member of Administrators group).  Is that at all portable or reliable?

What other ways are there?

Thanks,
Andrew

[1]
http://superuser.com/questions/660191/how-to-check-if-cygwin-mintty-bash-is-run-as-administrator


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list