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

后端 未结 13 1292
夕颜
夕颜 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条回答
  •  不思量自难忘°
    2020-12-04 08:12

    I like Rushyo's suggestion of using AT, but this is another option:

    whoami /groups | findstr /b BUILTIN\Administrators | findstr /c:"Enabled group" && goto :isadministrator
    

    This approach would also allow you to distinguish between a non-administrator and a non-elevated administrator if you wanted to. Non-elevated administrators still have BUILTIN\Administrators in the group list but it is not enabled.

    However, this will not work on some non-English language systems. Instead, try

    whoami /groups | findstr /c:" S-1-5-32-544 " | findstr /c:" Enabled group" && goto :isadministrator
    

    (This should work on Windows 7 but I'm not sure about earlier versions.)

提交回复
热议问题