Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Check for Administrator rights in Batch file.

3 Weeks Ago   #1
Paul Black

Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
 
 
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 System SpecsSystem Spec
.
3 Weeks Ago   #2
iko22

Windows 7 x64, Vista x64, 8.1 smartphone
 
 

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 System SpecsSystem Spec
3 Weeks Ago   #3
Paul Black

Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
 
 

Hi iko22,

Quote   Quote: Originally Posted by iko22 View Post
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!

Quote   Quote: Originally Posted by iko22 View Post
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 System SpecsSystem Spec
.

3 Weeks Ago   #4
Paul Black

Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
 
 

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 System SpecsSystem Spec
3 Weeks Ago   #5
Paul Black

Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
 
 

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.
My System SpecsSystem Spec
3 Weeks Ago   #6
iko22

Windows 7 x64, Vista x64, 8.1 smartphone
 
 

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 System SpecsSystem Spec
3 Weeks Ago   #7
Paul Black

Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
 
 

Hi iko22,

Quote   Quote: Originally Posted by iko22 View Post
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.
Quote   Quote: Originally Posted by iko22 View Post
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 System SpecsSystem Spec
3 Weeks Ago   #8
iko22

Windows 7 x64, Vista x64, 8.1 smartphone
 
 

Quote   Quote: Originally Posted by Paul Black View Post

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 System SpecsSystem Spec
3 Weeks Ago   #9
Paul Black

Win 7 HP SP1 64-bit Vista HB SP2 32-bit Linux Mint 18.3
 
 

Hi iko22,

Quote   Quote: Originally Posted by iko22 View Post
... 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 System SpecsSystem Spec
3 Weeks Ago   #10
iko22

Windows 7 x64, Vista x64, 8.1 smartphone
 
 

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 System SpecsSystem Spec
Reply

 Check for Administrator rights in Batch file.




Thread Tools




Similar help and support threads
Thread Forum
Remove Admin Rights and Move instead of Copy | Batch file
I have a list of 1000 files names that I put in a text file named list.txt. Need to find them in more folders, sub folders which has 12000+ files in it. The below script works fine but I would like the Admin Right to be removed and also I want the files to be moved permanently into C:\your_files....
General Discussion
shutdown in batch file requires Administrator
Hi all, I've been using GFI Backup 2009 for backups for a while, it does everything I want, and sends email reports after backup is complete. One of its features is to shutdown the computer after a backup, this rarley works, as it wont shutdown if a program is already running, including GFI...
General Discussion
Batch File on USB fails when Ran As Administrator
I am new to this site so please excuse any lack of etiquette I may show. I have recently built a .bat files to perform a large number of configuration changes needed for a Win7 or Win8 workstation to function properly on my network. The .bat file obviously needs to be ran with elevated...
Software
File permissions, administrator rights, sharing issues
We have been having an ongoing problem for several months. I will explain as best I can, but know that there are most likely more issues than what I am presenting here. I apologize in advance for my lengthiness. We have a home network of 7 computers and have been using Win 7 Pro and Office...
Microsoft Office
How to always run batch file as administrator?
I've made a batch file, but i don't know how to make it to always run as administartor. In .bat's file properties the option "always run as administartor" is grayed out. I have to right click it and then choose "run as administartor" from context menu. But i don't want to do it every time because...
General Discussion


Our Sites

Site Links

About Us

Find 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 00:12.
Twitter Facebook