How to check for admin rights ?
Moderator: DosItHelp
How to check for admin rights ?
How to do it correctly ?
Admin rights - it's mean when user prompts a UAC message and press OK button.
Admin rights - it's mean when user prompts a UAC message and press OK button.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: How to check for admin rights ?
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.
Re: How to check for admin rights ?
Ok. But what command you suggest to execute ?
Re: How to check for admin rights ?
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.
Re: How to check for admin rights ?
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).
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).
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: How to check for admin rights ?
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
)
Re: How to check for admin rights ?
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]
Re: How to check for admin rights ?
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.
Re: How to check for admin rights ?
Thanks, Compo.
It works great.
It works great.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: How to check for admin rights ?
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>
Re: How to check for admin rights ?
Sorry, but i don't understand, what do you talking about.
Bad english.
Bad english.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: How to check for admin rights ?
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.
-
- Posts: 233
- Joined: 21 Nov 2010 08:07
- Location: At My Computer
Re: How to check for admin rights ?
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
~DP
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
Re: How to check for admin rights ?
I remember introducing that method over at MDLDos_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>
Re: How to check for admin rights ?
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"