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

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

    I read many (most?) of the responses, then developed a bat file that works for me in Win 8.1. Thought I'd share it.

    setlocal
    set runState=user
    whoami /groups | findstr /b /c:"Mandatory Label\High Mandatory Level" > nul && set runState=admin
    whoami /groups | findstr /b /c:"Mandatory Label\System Mandatory Level" > nul && set runState=system
    echo Running in state: "%runState%"
    if not "%runState%"=="user" goto notUser
      echo Do user stuff...
      goto end
    :notUser
    if not "%runState%"=="admin" goto notAdmin
      echo Do admin stuff...
      goto end
    :notAdmin
    if not "%runState%"=="system" goto notSystem
      echo Do admin stuff...
      goto end
    :notSystem
    echo Do common stuff...
    :end
    

    Hope someone finds this useful :)

提交回复
热议问题