Check for Administrator rights in Batch file.

Page 1 of 2 12 LastLast

  1. Posts : 6,021
    Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
       #1

    Check for Administrator rights in Batch file.


    I basically want to check if the user has Administrator rights before executing the rest of the code. I want to have something like a bit of standalone code that can be added to the top of other scripts to check for Administrator rights first!

    Am I right in saying that this code . . .

    Code:
    FOR /F "tokens=1,2*" %%V IN ('bcdedit') DO SET adminTest=%%V
    IF (%adminTest%)==(Access) GOTO Not_Admin
    Call :Run_Go
     
    :Not_Admin
    Code goes here.
     
    :Run_Go
    Code goes here.
    . . . will not run Run_Go unless the user has elevated command privileges [Administrator rights]? Also, do I need an IF...ELSE in there instead of using Call?

    I have seen other code like this . . .

    Code:
    echo off
    goto check_Permissions
    :check_Permissions
        echo Administrative permissions required. Detecting permissions...
        net session >nul 2>&1
        if %errorLevel% == 0 (
            echo Success: Administrative permissions confirmed.
        ) else (
            echo Failure: Current permissions inadequate.
        )
        pause >nul
    . . . that I run on a machine with Administrator rights and it said that I didn't have Administrator rights!

    Any help will be appreciated.
      My Computer


  2. Posts : 2,798
    Windows 7 x64, Vista x64, 8.1 smartphone
       #2

    Hello @Paul Black,

    Lets start out by saying that Im baffled by the first code example in your question. It looks to me like a text processing routine on a string you called 'bcdedit', not very much to do with an elevated command prompt - or am i missing out on something?

    There is a difference between a User with Administration Rights and an Elevated Command Prompt. A User with Administration Rights still can start an ordinary cmd window, which is perhaps what you are getting in the second code example. The second code example requires net session to be run in an administrators command window, regardless of user privileges, otherwise it returns ACCESS DENIED.
      My Computer


  3. Posts : 6,021
    Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
    Thread Starter
       #3

    Hi iko22,

    iko22 said:
    Lets start out by saying that Im baffled by the first code example in your question. It looks to me like a text processing routine on a string you called 'bcdedit', not very much to do with an elevated command prompt - or am i missing out on something?
    That example code [snippet] was from here [which has to be run as right-click and run as an Administrator] => Event Viewer. I realise that they are two entirely different animals!

    iko22 said:
    There is a difference between a User with Administration Rights and an Elevated Command Prompt. A User with Administration Rights still can start an ordinary cmd window, which is perhaps what you are getting in the second code example. The second code example requires net session to be run in an administrators command window, regardless of user privileges, otherwise it returns ACCESS DENIED.
    I am basically after a way of checking [before any code is run] whether the user is using an Elevated Command Prompt.

    EDIT:
    [1] The first code in the link above runs OK directly from double-clicking the .bat file without me having to right-click and run as an Administrator.
    [2] The second code [check_Permissions] doesn't work if I double-click the .bat file OR if I right-click and run as an Administrator!
      My Computer


  4. Posts : 6,021
    Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
    Thread Starter
       #4

    Hi iko22,

    So if I had something like this at the top of the script/code . . .

    Code:
    FOR /F "tokens=1,2*" %%V IN ('bcdedit') DO SET adminTest=%%V
    IF (%adminTest%)==(Access) (GOTO Not_Admin) ELSE (GOTO :Run_Go)
    . . . does this test for a User with Administration Rights or does this test to see if it is being run in an Elevated Command Prompt Window, or am I completely missing the concept?

    I am basically after two options:

    [1] A way of checking whether the user is an Administrator when a script needs to be run as an Administrator.
    [2] A way of checking whether the user is using an Elevated Command Prompt when a script needs to be run in an Elevated Command Prompt .

    Thanks in advance.
      My Computer


  5. Posts : 6,021
    Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
    Thread Starter
       #5

    Hi iko22,

    I have done some investigating [and put the code below together] and I believe that the below code checks whether or not the batch file is being run from an elevated command prompt [Run as administrator] . . .

    Code:
    @echo off
    openfiles >NUL 2>&1 
    if NOT %ERRORLEVEL% EQU 0 goto NotAdmin 
    echo Hello, this command prompt is an elevated command prompt (Run as administrator).
    goto End
    :NotAdmin 
    echo This command prompt is NOT ELEVATED (NOT Run as administrator). 
    :End
    pause
    . . . is this correct?

    Thanks in advance.
    Last edited by Paul Black; 02 Jul 2019 at 08:29. Reason: Added Information!
      My Computer


  6. Posts : 2,798
    Windows 7 x64, Vista x64, 8.1 smartphone
       #6

    Hello Paul,

    Yes, it seems to do the trick alright. I tried the routine on my computer. The batch file once ran as administrator and once ran as default cmd prompt, and the logical output was conveyed to the user, in both cases.

    I do not know if it will work on all versions of Windows. Apparently, the trick does not work with 32 bit installers running on 64-bit computers (see: Openfiles - Windows CMD - SS64.com). In that situation, the advice is to try a similar trick around the NET SESSION command. But you say you have already tried to do similar, without much joy?
      My Computer


  7. Posts : 6,021
    Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
    Thread Starter
       #7

    Hi iko22,

    iko22 said:
    Yes, it seems to do the trick alright. I tried the routine on my computer. The batch file once ran as administrator and once ran as default cmd prompt, and the logical output was conveyed to the user, in both cases.
    Thanks for testing that and reporting the results.
    iko22 said:
    I do not know if it will work on all versions of Windows. Apparently, the trick does not work with 32 bit installers running on 64-bit computers (see: Openfiles - Windows CMD - SS64.com). In that situation, the advice is to try a similar trick around the NET SESSION command. But you say you have already tried to do similar, without much joy?
    I tested it on a 32-bit & 64-bit computer and it works fine.

    I will have another look at the NET SESSION command and see if I can get it to work!

    It is strange though because I have Administrator rights and the second code in post #2 when I ran it said that I didn't have Administrator rights [Failure: Current permissions inadequate.]!

    Thanks again.
      My Computer


  8. Posts : 2,798
    Windows 7 x64, Vista x64, 8.1 smartphone
       #8

    Paul Black said:

    I have seen other code like this . . .

    Code:
    echo off
    goto check_Permissions
    :check_Permissions
        echo Administrative permissions required. Detecting permissions...
        net session >nul 2>&1
        if %errorLevel% == 0 (
            echo Success: Administrative permissions confirmed.
        ) else (
            echo Failure: Current permissions inadequate.
        )
        pause >nul
    . . . that I run on a machine with Administrator rights and it said that I didn't have Administrator rights!

    Any help will be appreciated.
    ... it is because the routine is checking for the wrong errorlevel. According to this NET SESSION, the routine should check for errorlevel == 5.
      My Computer


  9. Posts : 6,021
    Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
    Thread Starter
       #9

    Hi iko22,

    iko22 said:
    ... it is because the routine is checking for the wrong errorlevel. According to this NET SESSION, the routine should check for errorlevel == 5.
    I have just tried that on my 32-bit computer and it still says that I don't have Administrator rights [Failure: Current permissions inadequate.]!
      My Computer


  10. Posts : 2,798
    Windows 7 x64, Vista x64, 8.1 smartphone
       #10

    Rather confusingly, I got the routine to work with NET SESSION. Two points to make: 1) It only works (recognises the errorlevel) if >nul 2>&1 is removed; and 2) The routine outputs System Error 5, but it has to check on errorlevel 2 and errorlevel 0!!

    See batch code below:

    Code:
    echo off
    
        echo Administrative permissions required. Detecting permissions...
        net session
        pause
        if %errorLevel%  == 0 (
            echo Success: Administrative permissions confirmed.
    	pause
        ) 
        if %errorlevel% == 1 (
    	pause
        )
        if %errorlevel% == 2 (
            echo Failure: Current permissions inadequate.
    	pause
        )
        echo %errorlevel%
        pause
      My Computer


 
Page 1 of 2 12 LastLast

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 02:19.
Find Us