In chapter 4 of the Batchography book, I illustrate 3 methods that you can use to check if your Batch file script is running as an Administrator.
In this blog post, I will share with you the method entitled “Using known commands that fail to run without elevated privileges” from the book.
The idea behind this method is to run commands that are known to fail and return a non-zero error code if they are not executed with administrative privilege.
The Batch script would then check the return code and act accordingly:
@echo off :: :: The Batchography book by Elias Bachaalany :: net session >nul 2>&1 if %errorlevel% neq 0 ( echo Requires administrative privilege. goto :eof ) echo Script starts here...
Look no further, the Batchography is the right book for you.
You might also like: