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

后端 未结 13 1313
夕颜
夕颜 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

    Here's a slight modification of Harry's answer that focuses on elevated status; I'm using this at the start of an install.bat file:

    set IS_ELEVATED=0
    whoami /groups | findstr /b /c:"Mandatory Label\High Mandatory Level" | findstr /c:"Enabled group" > nul: && set IS_ELEVATED=1
    if %IS_ELEVATED%==0 (
        echo You must run the command prompt as administrator to install.
        exit /b 1
    )
    

    This definitely worked for me and the principle seems to be sound; from MSFT's Chris Jackson:

    When you are running elevated, your token contains an ACE called Mandatory Label\High Mandatory Level.

提交回复
热议问题