Is there a way to run/start cmd as administrator through the command line or a batch file programming in Windows 8?
I want to create a batch file which has administrative privileges without any prompt to the user.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityIs there a way to run/start cmd as administrator through the command line or a batch file programming in Windows 8?
I want to create a batch file which has administrative privileges without any prompt to the user.
runas /profile /user:administrator “Driver:\folder\program”
For example, the administrator account is “AAA” and you want to run BBB.exe of C:\programs, you should follow these steps:
Hope it works.
REM --> Check for permissions
>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"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
You can download already created portable and clean (generated in the Windows XP
) shortcut files set: https://sourceforge.net/p/contools/contools/HEAD/tree/trunk/Scripts/Tools/ToolAdaptors/lnk/
Or even generate your own: https://sourceforge.net/p/contools/contools/HEAD/tree/trunk/Scripts/Tools/ToolAdaptors/vbs/
Usage example:
cmd_admin.lnk /C ...
Each lnk file just a link to the cmd.exe
, so you can pass here all the cmd.exe
command line options.
Pros:
runas
method.Cons:
cmd.exe
process. To start any other process you have to either run it from the cmd.exe
script, or create another standalone shortcut with the Run as Administrator
flag raised..lnk
) in the Windows XP (but not in the Windows 7) brings truncated command line down to ~260 characters..lnk
) loads console windows parameters (font, windows size, buffer size, etc) from the shortcut at first and from the registry (HKCU\Console) at second. If try to change and save parameters, then it will be saved ONLY into the shortcut, which brings the shortcut file overwrite.