MATLAB Answers

M-script to determine if user has administrator rights

12 views (last 30 days)
Jeremy on 15 Aug 2016
Commented: Walter Roberson on 3 Nov 2019
I am seeing an issue where my script crashes for some users on some machines, because that user does not have administrative rights on that machine. Is there any way I can check in the m-script to see if the user has admin rights? I'm not asking if the user is running MatLab as an administrator, but if the user has the ability to run other programs (say a compiler which will be called by a build process later in the script) as an administrator.

Answers (3)

Guillaume on 15 Aug 2016
Because access permission is such a complex beast on Windows, the best way to check if the user can do something is simply to attempt to do the something and check that it succeeded:
%do operation that may fail due to insufficient permissions
%operation failed, handle any way you want, e.g.:
error('You do not have sufficient permissions to perform the operation. Log in as admin and try again.');
%rest of program ...
Note that being administrator may or may not be sufficient to do whatever you want to do and note that even non-administrators may be able to do it. If you really wanted to check before hand if the user can do your operation you would check for the exact permission required among the many possible permissions.
In any case, there is no tool in matlab to manage / check user permissions. If you really want to check permissions, on Windows, you would have to defer to .Net, for example the < WindowsPrincipal class. I've not been able to work out how to use it reliably, unfortunately.
As said, try... catch is the best way to deal with this.

raym on 18 May 2019
function out = isadmin
out = System.Security.Principal.WindowsPrincipal(...
  1 Comment
Walter Roberson
Walter Roberson on 18 May 2019
Interesting, but I would have thought that this is testing whether the user is using Run As Administrator, which the original poster indicates is not quite what they need

Sign in to comment.

Syed Owais
Syed Owais on 3 Nov 2019
i am trying to add a function in matlab through edit windows but when i save it shows message that you dont have administrative rights to do so what should i do
  1 Comment
Walter Roberson
Walter Roberson on 3 Nov 2019
You should rarely rarely save a function underneath the MATLAB installation directory.
About the only exception I have come across is that there is provision for a site-specific startup file that it would make sense for systems administrators to configure. However, in such a case the systems administrators would be able to grant themselves access to write the file.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!