Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

Running a Batch File as an Admin

Hey HF!

Any possible way to make a batch file without right clicking and pressing "Run As Administrator".

That's all! Thanks guys!
Can you clarify exactly what you're seeking to do? A handful of extra words or a screenshot goes a long way. Don't be afraid to describe the problem twice, in slightly different ways. :>

The wording as is implies that when you try to right-click your desktop and point to "New", "Text Document", and edit in notepad, add code and click save, you get a UAC prompt.
Is that what is happening? If so, it might be because you're trying to create/save a batch file in a protected folder like C:\Windows or C:\
Try again in a folder like C:\scripts\

Another thing you might mean:
Perhaps you mean that you want a batch file to launch some software and automatically accept the UAC prompt.
Is that it? If that's the case, you're out of luck; the UAC prompt is designed to not be accessible programatically, because it's designed to differientiate between human-initiated actions and computer-initiated actions such as, for example, a malicious script. Your best option would be to disable UAC.

Another thing you might mean:
Perhaps you mean that you want to trigger a UAC prompt, relaunching the batch file with elevated permissions.
Is that it? If so, then you can't do it directly from batch, but it's possible via vbscript, which you can store within the batch file and "echo out" when it's needed.
I can't remember the vbscript off the top of my head so let me know if this is it, and I'll write about it.
This is what I use. It was posted on stack overflow several years ago and I have since managed to lose who the original author was. It checks for elevated permission by checking if net file works. If it doesn't work then it creates a .vbs script that starts the batch file elevated causing the UAC prompt to come up.

Code
::::::
:: Automatically check & get admin rights
::::::
@echo off
CLS
ECHO.
ECHO ======
ECHO Running Admin shell
ECHO ======

:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO.
ECHO ******
ECHO Invoking UAC for Privilege Escalation
ECHO ******

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B

:gotPrivileges
::::::
:START
::::::
setlocal & pushd .

REM Run shell as admin (example) - put here code as you like
There's no place like ~/
(Feb 10, 2015 01:10 PM)b00st3d Wrote:  This is what I use. It was posted on stack overflow several years ago and I have since managed to lose who the original author was. It checks for elevated permission by checking if net file works. If it doesn't work then it creates a .vbs script that starts the batch file elevated causing the UAC prompt to come up.

Code
::::::
:: Automatically check & get admin rights
::::::
@echo off
CLS
ECHO.
ECHO ======
ECHO Running Admin shell
ECHO ======

:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO.
ECHO ******
ECHO Invoking UAC for Privilege Escalation
ECHO ******

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B

:gotPrivileges
::::::
:START
::::::
setlocal & pushd .

REM Run shell as admin (example) - put here code as you like

This is what i needed! Thanks! Sorry if I was not very clear in the post! 🤝


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by Robert RougeSep 24, 2026 10:02 AM 4 220 Sep 24, 2026 10:02 AM
Last Post: Anonynuk
[Shell] by fisica638Oct 30, 2019 11:05 AM 1 536 Oct 30, 2019 11:05 AM
Last Post: Hackoo
[.NET] by Takehito MorokuzuMay 5, 2018 12:21 PM 9 371 May 5, 2018 12:21 PM
Last Post: He B TeMy