How do I check if the current batch script has admin rights?

I know how to make it call itself with runas but not how to check for admin rights. The only solutions I've seen are crude hack jobs.

link|improve this question
@Bobby: That was asking how to do it in bash though. – paradroid Oct 29 '10 at 12:56
@Jason404: Wtf?! How the hack did I misread Batch for Bash? oO' @Tilka: My sincere apologies. – Bobby Oct 29 '10 at 13:04
feedback

2 Answers

up vote 4 down vote accepted

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.

link|improve this answer
feedback

This is the best kludge I could think of, using standard commands:

net user %username% | findstr /r Administrator.
if %errorlevel% == 1 (
echo This is not an admin account
) else (
echo This is an admin account
)
link|improve this answer
That doesn't work when you are in a domain. And when I tried "net user %username% /domain" the Local Group Membership section didn't contain any groups although I have local admin rights. Strange... – Tilka Oct 29 '10 at 13:19
Plus the output is localized. – Tilka Oct 29 '10 at 13:22
Besides, one can run a batch file without administrator rights from an account who is in Administrators group (this especially relates Vista/7). – utapyngo Nov 9 '11 at 4:41
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.