How to check for admin rights ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

How to check for admin rights ?

#1 Post by Dragokas » 12 Jul 2014 07:25

How to do it correctly ?

Admin rights - it's mean when user prompts a UAC message and press OK button.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to check for admin rights ?

#2 Post by ShadowThief » 12 Jul 2014 10:00

Code: Select all

at


If you don't have admin rights, %errorlevel% will be 1. If you do have admin rights, %errorlevel% will be 0.

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to check for admin rights ?

#3 Post by Dragokas » 12 Jul 2014 11:14

Ok. But what command you suggest to execute ?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to check for admin rights ?

#4 Post by foxidrive » 12 Jul 2014 11:31

Dragokas wrote:How to do it correctly ?

Admin rights - it's mean when user prompts a UAC message and press OK button.



What are you trying to do?

A user with admin permissions still has to elevate some tasks, and doesn't get a UAC message if UAC is disabled.

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to check for admin rights ?

#5 Post by Dragokas » 12 Jul 2014 13:16

Ok. My code must be run with admin privileges.
I have a function in my code to prompt user for an elevation with UAC message.

But there are two ways to run my.cmd:

1) Double click
2) Right Click -> run with admin privileges

In the second case i do not need to call Elevate function.
That's why first I must to check whether my code runned with admin privileges already.

I can't check it with file commands like echo.>"%SystemRoot%\Evelate_test"
or registry commands like reg add hklm\software\elevate_test
because there is little chance that the rights for a system folder or hklm hive will be broken (example - with virus infection).

I need a reliable check that will be work on any win-systems (Vista and above).

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to check for admin rights ?

#6 Post by ShadowThief » 12 Jul 2014 13:20

Dragokas wrote:Ok. But what command you suggest to execute ?

The command is literally called

Code: Select all

at


And then

Code: Select all

if %errorlevel%==1(
    echo Not admin
) else (
    echo admin
)

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to check for admin rights ?

#7 Post by Dragokas » 12 Jul 2014 13:43

not suitable.

Code: Select all

C:\Windows\system32>at
The AT command has been deprecated. Please use schtasks.exe instead.
The request is not supported.

C:\Windows\system32>echo %errorlevel%
1

C:\Windows\system32>ver
Microsoft Windows [Version 6.3.9600]

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: How to check for admin rights ?

#8 Post by Compo » 12 Jul 2014 13:57

Try this!

Code: Select all

@Echo Off
Net Session >Nul 2>&1||Call :ElevateFunction
Rem Already Elevated code Here
<SNIP />
Exit/B
:ElevateFunction
<SNIP />
Last edited by Compo on 13 Jul 2014 11:07, edited 1 time in total.

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to check for admin rights ?

#9 Post by Dragokas » 12 Jul 2014 14:08

Thanks, Compo.
It works great.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to check for admin rights ?

#10 Post by ShadowThief » 12 Jul 2014 14:13

Sorry, I didn't realize you wanted me to straight-up GIVE you the code.

Code: Select all

@echo off
at>nul

if %errorlevel%==1 (
   Call :ElevateFunction
)

<rest of your code goes here>

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to check for admin rights ?

#11 Post by Dragokas » 12 Jul 2014 14:16

Sorry, but i don't understand, what do you talking about.
Bad english.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to check for admin rights ?

#12 Post by ShadowThief » 12 Jul 2014 14:22

Oh yeah, they got rid of at in Windows 8, didn't they... I wish you had mentioned that that's what you were running; I wouldn't have suggested it in the first place.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: How to check for admin rights ?

#13 Post by Dos_Probie » 18 Jul 2014 05:28

I know this is now 5 days old but since you are running 8.1, this is what I use to force the end user to right-click when running as a non-admin, if admin it will just continue on.

~DP 8)

Code: Select all

:: ### AdminCheck
 reg query "HKU\S-1-5-19" >NUL 2>&1 && (
 goto admin
 ) || (
        @color 4f
        mode con: cols=80 lines=4
        title, ## Windows 8.1 ADMIN CHECK ##
   ECHO= Right-Click "%~nx0" And Run as Administrator!
   ECHO=
   timeout /t 4 /nobreak>nul
        exit)

:admin
echo You are running this OS As the Built-In Administrator.
echo.
::<Code Here>
pause

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: How to check for admin rights ?

#14 Post by Compo » 18 Jul 2014 09:13

Dos_Probie wrote:I know this is now 5 days old but since you are running 8.1, this is what I use to force the end user to right-click when running as a non-admin, if admin it will just continue on.

~DP 8)

Code: Select all

:: ### AdminCheck
 reg query "HKU\S-1-5-19" >NUL 2>&1 && (
 goto admin
 ) || (
<SNIP>
I remember introducing that method over at MDL

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to check for admin rights ?

#15 Post by Dragokas » 18 Jul 2014 09:28

I like one more code from there... with little change:

Code: Select all

set "Priv=OK" & ver |>NUL find "6." && WHOAMI /PRIV |>NUL find /i "SeTakeOwnershipPrivilege" || set "Priv=BAD"

Post Reply