This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
189} How do I test if the current user has administrator rights?
Entirely due to others:
@echo off & setlocal enableextensions
net localgroup administrators|findstr /b /e "%USERNAME%" > nul
if %errorlevel% EQU 0 (
echo The current user %USERNAME% has administrator rights
) else (
echo The current user %USERNAME% does not have administrator rights)
endlocal & goto :EOF
My own, similar
XP-only solution
@echo off & setlocal enableextensions
net session 2>&1 > nul
if %errorlevel% EQU 0 (
echo The current user %USERNAME% has administrator rights
) else (
echo The current user %USERNAME% does not have administrator rights)
endlocal & goto :EOF
Another, even simpler one. Also XP-only
@echo off & setlocal enableextensions
fsutil > nul
if %errorlevel% EQU 0 (
echo The current user %USERNAME% has administrator rights
) else (
echo The current user %USERNAME% does not have administrator rights)
endlocal & goto :EOF
For
Windows 7 and beyond
@echo off & setlocal enableextensions
net user %USERNAME%|findstr /i /c:"Local Group Memberships"|findstr /i /c:"*Administrators">nul
if %errorlevel% EQU 0 (
echo User %USERNAME% has administrator rights
) else (
echo User %USERNAME% does not have administrator rights)
endlocal & goto :EOF
Note that the solution is system language specific. E.g. in Finnish
substitute the following unlikely mix
net user %USERNAME%|findstr /i /c:"Local Group Memberships"|findstr /i /c:"*Järjestelmänvalvojat"