While working on automated provisioning of a Jenkins slave server on Windows, I needed
to verify that one of my batch scripts was running with administrative privileges.
Turns out this problem is easy to solve these days as long as you don’t need to support
XP. Thanks to and31415 on SO for the
great post on using fsutil!
Here’s a working example:
1234567891011121314151617181920
@ECHO OFFSETLOCAL ENABLEEXTENSIONS:: verify we have admin privilegesCALL :IsAdmin || (ECHO %~n0: ERROR - administrative privileges required && EXIT /B 1)ECHO "Hello, Admin!":EXITEXIT /B:: function to verify admin/UAC privileges:: CREDIT: http://stackoverflow.com/a/21295806/1995977:IsAdminIF NOT EXIST "%SYSTEMROOT%\system32\fsutil.exe" ( ECHO %~n0: WARNING - fsutil command not found; cannot verify adminstrative rights) ELSE ( "%SYSTEMROOT%\system32\fsutil.exe" dirty query "%SystemDrive%" >NUL 2>&1)EXIT /B