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

后端 未结 13 1288
夕颜
夕颜 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:21

    This trick only requires one command: type net session into the command prompt.

    If you are NOT an admin, you get an access is denied message.

    System error 5 has occurred.
    
    Access is denied.
    

    If you ARE an admin, you get a different message, the most common being:

    There are no entries in the list.
    

    From MS Technet:

    Used without parameters, net session displays information about all sessions with the local computer.

提交回复
热议问题