C:\_G\WWW\~ELISANET\INFO\tscmd189.html
<http://www.elisanet.fi/tsalmi/info/tscmd189.html>
Copyright © 2013- by Prof. Timo Salmi  
Last modified Tue 25-Dec-2018 18:44:48

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

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

Windows 7 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"

References/Comments:
  Google Groups 14 Feb 2013 14:19:20 +1100
  Google Groups 14 Feb 2013 12:19:22 +0800
  Google Groups 14 Feb 2013 01:30:04 -0800

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd189.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd189
http://www.elisanet.fi/tsalmi/info/tscmd189.html
file:///c:/_g/www/~elisanet/info/tscmd189.html