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:
:: The Batchography book by Elias Bachaalany
net session >nul 2>&1
if %errorlevel% neq 0 (
echo Requires administrative privilege.
echo Script starts here...
Do you want to master Batch Files programming?
Look no further, the Batchography is the right book for you.
Available in print or e-book editions from Amazon.com!
You might also like: