Batch script: how to check for admin rights

You could always do something like this

mkdir "%windir%\system32\test" 2>nul
if "%errorlevel%" == "0" (rmdir "%windir%\system32\test" & echo Is admin) else (echo Not an Admin)

Not the best of ways but works for me all the time.


After some research (mostly on SO[1]), I found that net is not always a good solution, it can show false negative results. But there is fsutil available for WinXP to Win10. It's worth reading the whole insightful answer "More issues" to the question Batch script: how to check for admin rights there.

Here's the short answer for hurried users:

fsutil dirty query %systemdrive% >nul
if %errorlevel% == 0 (
    echo Running with admin rights.
) else (
    echo Running without
)

[1] my actual problem was elevation, but non-invasive checking is part of it:

  • command line - How to request Administrator access inside a batch file

  • windows - How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?

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

  • windows - Batch script: how to check for admin rights