To avoid execution problems in a program I need to know if Mathematica has been "Run as administrator". What would be the cleanest way to programmatically getting this information?

  • 1
    $\begingroup$ Which OS? Windows from the wording but can you confirm? $\endgroup$ Commented Jun 1, 2015 at 16:37
  • $\begingroup$ Yes, I work on Windows. $\endgroup$ Commented Jun 1, 2015 at 16:43
  • 1
    $\begingroup$ See this if you want a command line method: (4051883) $\endgroup$
    – Mr.Wizard
    Commented Jun 1, 2015 at 17:33
  • $\begingroup$ @Mr.Wizard: Your response is clearly an answer to the question. I use Mathematica as much as possible, so I would rather not have to go somewhere else for a solution. In other words, maybe your code should be packed in a function (RunningAsAdministratorQ) and included as part of the Wolfram Language. $\endgroup$ Commented Jun 1, 2015 at 17:38
  • 1
    $\begingroup$ I don't think that Mathematica has this built in (one never knows as there is a lot of stuff which is not documented or hard to find). So I think you can either a) use a indirect method as Mr.Wizard has suggested (which might of course give a wrong answer, e.g. when someone has played with permissions of $InstallationDirectory), you can b) fire a command line tool (also suggested already) or use NETLink to access the corresponding Windows API call (which I don't know but certainly exists...) $\endgroup$ Commented Jun 3, 2015 at 20:49

3 Answers 3


The only method I am aware of is to attempt an operation that requires administrator-level permissions and look for a General::privv message:

  (CreateDirectory @ #; DeleteDirectory @ #) & @ 
   FileNameJoin[{$InstallationDirectory, "test"}]; True,
] // Quiet

This should return True if it is run with administrator control and False if not.

  • $\begingroup$ This is a clean workaround. I was hoping for a function call though. $\endgroup$ Commented Jun 1, 2015 at 16:51
  • $\begingroup$ @Ariel Quite likely there is a better way but at least this works in the mean time. $\endgroup$
    – Mr.Wizard
    Commented Jun 1, 2015 at 17:32
  • 3
    $\begingroup$ +1 This is a nice direct method. Funny story: I once ran into a case where a very locked-down corporate virus scanner unceremoniously terminated my program when it tried to use a similar technique. $\endgroup$
    – WReach
    Commented Jun 6, 2015 at 23:46
  • 1
    $\begingroup$ @ArielSepulveda Well, those are function calls you're looking at. If you want just a single function call, wrap a function around that code :) $\endgroup$ Commented Feb 3 at 18:00

It may suit your purpose to use the Windows API function IsUserAnAdmin. It can be accessed through NETLink like this:

isUserAnAdmin = DefineDLLFunction["IsUserAnAdmin", "shell32.dll", "bool", {}];

(* True if Mathematica was launched elevated, False otherwise *)

It is also possible to to use the shell command whoami to determine the Mandatory Label which, in turn, can tell us if the current session is running elevated:

isElevated[] :=
    Import["!whoami /groups | find \"Mandatory Label\"", "Text"]
  , "S-1-16-12288"|"S-1-16-16384"

(* True if Mathematica was launched elevated, False otherwise *)

The magic strings in isElevated are the security identifiers (SIDs) denoting the elevated integrity levels. This text-based solution has the advantage that it does not require NETLink to be loaded. On the other hand, it is more brittle should the output format of whoami ever be changed.


Here's how you can do it using IsUserAnAdmin SHELL32 function on Windows, without using NETLink:

isUserAnAdmin = ForeignFunctionLoad[
 Environment["SystemRoot"] <> 
  "/System32/shell32.dll", "IsUserAnAdmin", {} -> "CUnsignedChar"];


The Windows system ABI has a BOOL same size as a CHAR.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.