Launch a bat file without a command window as administrator?
Solution 1
You can use the ShellExecute method of the Windows Shell object instead, and use the runas
operation.
Set Shell = CreateObject("Shell.Application")
Shell.ShellExecute "Start.bat", , , "runas", 0
This will request elevation and run Start.bat
.
ShellExecute's arguments are (excerpted and summarized from the ShellExecute page on MSDN):
- sFile [in] - String of the filename to perform the operation on
- vArguments [in, optional] - String of arguments (command line arguments)
- vDirectory [in, optional] - The fully qualified path of the directory that contains the file specified by sFile. If this parameter is not specified, the current working directory is used.
- vOperation [in, optional] - The operation to be performed. If this parameter is not specified, the default operation is performed.
- vShow [in, optional] - Initial window display recommendation. 0 for hidden.
If you absolutely have to use cmd /c
to run the batch file, you'll need to specify the full path to it. The invocation would look something like this:
Set Shell = CreateObject("Shell.Application")
Shell.ShellExecute "cmd", "/c F:\ull\path\to\Start.bat", , "runas", 0
Solution 2
You can add this code to the top of the batch file and it will self request admin rights:
@echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
<YOUR BATCH SCRIPT HERE>
From here: https://stackoverflow.com/a/10052222/5518385
Related videos on Youtube
POPCORNS
jdjfkfk k jdj k kdjdj kfjdk j kdjdkfj kdjfkdjfk jdj jd j jdjdj kfk k kfkdj kdjfkdj j j jdjdjfkdjfkdj kdjdk jfkdjfkfj kdj kdjfkdj kdjdkfkdjdkdjdj jfkdjfkdjdjd jfjdjfjdj jsdj jdkjdkjfkdjfkdj kfkfkfk jf kfkfjfk k k fkfjdj jdjdj jdxj jdjfk jdkdjfk jdjdj jfjdkfj
Updated on September 18, 2022Comments
-
POPCORNS over 1 year
I've tried many solutions, but they didn't work, somehow.
I can currently launch a bat file without command window from a vbs but I don't know how to launch it as admin.
VBScript (So I can launch the batch file without a command window):
Set oShell = CreateObject ("Wscript.Shell") Dim strArgs strArgs = "cmd /c Start.bat" oShell.Run strArgs, 0, false
Batch (Start.bat):
Start /wait Application.exe Net stop ServiceNameGoesHere
How do I launch the batch file as administrator while still making it invisible?
-
POPCORNS over 7 yearsTried this, after the UAC prompt a command window popped out. I want it to be invisible. And I don't want to use task scheduler for that.
-
ganesh over 7 yearsthe
start
command has a/min
option. If you add that the cms windows will still open when thet batch-file runs, but it will be minimized to the taskbar. -
POPCORNS over 7 yearsThis is one of the solutions that I want to avoid as well because I don't want myself to accidently close the command prompt.
-
Yisroel Tech over 7 yearsWorks like a charm! (what would be the benefit of using cmd /c?)
-
Dranon over 7 years@YisroelTech In this case, or in the case of directly running an executable, none, however I included it as an option since the OP was using it. If your command is a shell builtin (e.g.
copy
) or uses shell features like redirection (e.g.someprogram > file.txt
) then you need to use it. -
Dranon over 7 yearsIf you change the
1
after the"runas"
to0
, it'll hide the console window for the elevated version (but not, of course, for the original invocation). Depending on what the OP wants (single batch file, pair of related files), either my answer or yours, or some combination of the two should work since the way we're requesting elevation is the same. -
POPCORNS over 7 yearsThanks! I can't find a solutuion anywhere else but here.
-
Dranon about 7 years@UltimatePOPCORNS If you found this answer solved your problem, please mark it as accepted. I would also suggest that you take another look at the answer by Yisroel Tech combined with my comment underneath it, just in case that's more of what you want.
-
Laurie Stearn over 4 yearsThe SO answer had been updated since- sadly didn't work for me, but this did- again with the
"runas", 0