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

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

    Pretty much what others have put before, but as a one liner that can be put at the beginning of a batch command. (Well, usually after @echo off.)

    net.exe session 1>NUL 2>NUL || (Echo This script requires elevated rights. & Exit /b 1)
    

提交回复
热议问题