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
Here’s a working example:
:: verify we have admin privileges
CALL :IsAdmin || (ECHO %~n0: ERROR - administrative privileges required && EXIT /B 1)
ECHO "Hello, Admin!"
:: function to verify admin/UAC privileges
:: CREDIT: http://stackoverflow.com/a/21295806/1995977
IF 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
Shameless plug – learn more tips and tricks for batch scripting in my Guide to Windows Batch Scripting!