How to detect if CMD is running as Administrator/has elevated privileges?

后端 未结 13 1317
夕颜
夕颜 2020-12-04 07:51

From inside a batch file, I would like to test whether I\'m running with Administrator/elevated privileges.

The username doesn\'t change when \"Run as Administrator\

13条回答
  •  旧时难觅i
    2020-12-04 08:12

    The easiest way to do this on Vista, Win 7 and above is enumerating token groups and looking for the current integrity level (or the administrators sid, if only group memberhip is important):

    Check if we are running elevated:

    whoami /groups | find "S-1-16-12288" && Echo I am running elevated, so I must be an admin anyway ;-)
    

    Check if we belong to local administrators:

    whoami /groups | find "S-1-5-32-544" && Echo I am a local admin
    

    Check if we belong to domain admins:

    whoami /groups | find "-512 " && Echo I am a domain admin
    

    The following article lists the integrity level SIDs windows uses: http://msdn.microsoft.com/en-us/library/bb625963.aspx

提交回复
热议问题