The cleanest way to check for admin privileges using a CMD script, that I have found, is something like this:
@echo off
REM Calling verify with no args just checks the verify flag,
REM we use this for its side effect of setting errorlevel to zero
verify >nul
REM Attempt to read a particular system directory - the DIR
REM command will fail with a nonzero errorlevel if the directory is
REM unreadable by the current process. The DACL on the
REM c:\windows\system32\config\systemprofile directory, by default,
REM only permits SYSTEM and Administrators.
dir %windir%\system32\config\systemprofile >nul 2>nul
REM Use IF ERRORLEVEL or %errorlevel% to check the result
if not errorlevel 1 echo has Admin privs
if errorlevel 1 echo has only User privs
This method only uses CMD.exe builtins, so it should be very fast. It also checks for the actual capabilities of the process rather than checking for SIDs or group memberships, so the effective permission is tested. And this works as far back as Windows 2003 and XP. Normal user processes or nonelevated processes fail the directory probe, where as Admin or elevated processes succeed.
1Thanks but I don't see any difference between how UAC elevated and non-elevated processes are being displayed. For example, I've started one calc.exe with and one without elevation, and "tasklist /v | findstr calc" is telling me:
.
calc.exe 3988 Console 1 12,916 K Running compname\username 0:00:00 Calculator
.calc.exe 4704 Console 1 12,924 K Running compname\username 0:00:00 Calculator
– kazhtaco – 2015-06-30T12:55:24.173