5

I am writing a batch file that I will distribute among users. I need to run some commands with elevated permissions. My initial solution was to use:

runas /noprofile /user:Administrator SOME_COMMAND  

However many machines (including mine) have the hidden Administrator account inactive and so do not have an administrator password set up. I can also not specify another user name since different machines will have different users. Is there a way to replicate the "right click -> Run as Administrator" action via a batch file? I do not mind the prompt popping up, I just do not want the user to explicitly right-click and run as administrator.

5

You can take advantage of PowerShell, which is present on every modern Windows system.

Split the commands that need elevation off into a separate batch file, e.g. elevated.bat. Then, when it's time to run them, use this from your non-elevated script:

powershell -command "Start-Process elevated.bat -Verb runas"

The -Verb runas part is what causes the elevation prompt. If the original batch file is already running as admin, or if UAC prompts are off, the new file will be elevated without a prompt.

Note that the elevated batch processor's current directory will start out as System32. If that's a problem, you can use this alternate version to have it start in the same directory as the non-elevated script:

powershell -command "Start-Process cmd -ArgumentList '/c %CD% && elevated.bat' -Verb runas"

That causes the new cmd instance to first cd into the directory provided by the unelevated prompt's %CD% variable, then execute the desired batch file.

  • Thanks. However powershell scripts are disabled by default on almost all machines. Right click->run as Admin on batch files would be more convenient in that case than first having users enable powershell scripts. – hazrmard Aug 13 '16 at 1:25
  • 1
    @hazrmard This doesn't require script execution to be enabled, since you're just running a command, not a script file. (I just checked and -command can indeed be used even if scripts can't.) – Ben N Aug 13 '16 at 1:27
  • True. But I am distributing a batch file among users. Which means I'll have to ask users to first type in this command in power shell to run the batch file. I don't even trust my users to Right click-> Run as admin. Hence my question in the first place :) – hazrmard Aug 13 '16 at 1:30
  • 1
    @hazrmard You're not running a PowerShell command, you're invoking powershell.exe from your batch script to automatically run one PS command. The user will never even know PowerShell is involved. – Ben N Aug 13 '16 at 1:31
  • Splendid. Works for cmd itself as well: powershell -command "Start-Process cmd.exe -Verb runas" – Jeroen Wiert Pluimers Aug 14 '18 at 10:14
1

What I use is the following code:

::#################################################################################################################################
:: Elevate this script                                                                                                            #
::#################################################################################################################################

(
    :: Check Admin rights and create VBS Script to elevate
    >nul fsutil dirty query %SYSTEMDRIVE% 2>&1 || (

        :: Very little red console
        mode con cols=80 lines=3 
        color cf

        :: Message
        title Please wait...
        echo.
        echo                         Requesting elevated shell...

        :: Create VBS script
        echo Set UAC = CreateObject^("Shell.Application"^)>"%TEMP%\elevate.vbs"
        echo UAC.ShellExecute "%~f0", "%TEMP%\elevate.vbs", "", "runas", 1 >>"%TEMP%\elevate.vbs"
        if exist "%TEMP%\elevate.vbs" start /b /wait >nul cscript /nologo "%TEMP%\elevate.vbs" 2>&1

        :: Delete elevation script if exist
        if exist "%TEMP%\elevate.vbs" >nul del /f "%TEMP%\elevate.vbs" 2>&1

        exit /b
    )    
)

pushd "%~dp0"

.... your code ....

popd

Put it after your @echo off and remarks.

0

RunAdmin lets you run a program from command line with elevated rights (it will show the UAC so the user can enter credentials).

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.