Batch script: check for admin rights

After some extensive research and testing, I have found the most accurate way to check for administrative privileges in batch script is:

@echo off

net session >nul 2>&1
if NOT "%errorlevel%"=="0" (
  echo THIS SCRIPT IS NOT ADMINISTRATIVE
  ping -n 2 127.0.0.1 >nul 2>&1
  exit
)

echo THIS SCRIPT IS ADMINISTRATIVE
ping -n 2 127.0.0.1 >nul 2>&1

No comments:

Post a Comment