Create batch file to run cmd as admin and then run two commands

Create batch file to run cmd as admin and then run two commands

To create a batch file that runs Command Prompt (cmd) as an administrator and then executes two commands, you'll need to use a combination of techniques. Batch files alone cannot elevate privileges directly, but you can use a vbs script to help with the elevation.

Here's a step-by-step guide:

Step 1: Create the Batch File

First, create a batch file (e.g., run_as_admin.bat) that will be executed with administrator privileges. This file will call a vbs script for elevation and then run the two commands.

@echo off :: Check for admin rights :: If the script is not running as admin, relaunch it with admin rights open_vbs.bat exit /b :: Commands to run as admin echo Running commands as admin... echo. :: Your commands here echo Command 1 echo Command 2 

Step 2: Create the vbs Script

Create a VBScript file (e.g., open_vbs.bat) to handle the elevation:

@echo off setlocal set "batchPath=%~f0" :: Check if the script is running as admin open_vbs.vbs "%batchPath%" exit /b 

And the open_vbs.vbs file:

Set UAC = CreateObject("Shell.Application") UAC.ShellExecute "cmd.exe", "/c """ & WScript.Arguments(0) & """", "", "runas", 1 

Explanation:

  1. run_as_admin.bat:

    • Checks if it's running with admin rights. If not, it calls the VBScript to run the batch file as an administrator.
    • Executes your commands.
  2. open_vbs.bat:

    • Calls the VBScript to relaunch the batch file with administrative privileges.
  3. open_vbs.vbs:

    • Uses ShellExecute with the runas verb to run the batch file as an administrator.

Example Commands

Replace echo Command 1 and echo Command 2 with the actual commands you want to run. For instance:

:: Commands to run as admin echo Running commands as admin... echo. :: Example commands dir C:\ echo Hello, World! 

Summary

This approach allows you to:

  • Elevate privileges using VBScript.
  • Execute multiple commands from a batch file with administrative rights.

Make sure you save the .bat and .vbs files in the same directory and execute the run_as_admin.bat file to start the process.

Examples

  1. "Create batch file to run commands as administrator"

    • Description: A batch file that uses runas to execute commands as an administrator.
    • Code:
      @echo off :: Request elevation setlocal set "batchPath=%~f0" set "batchArgs=%*" if not "%1"=="elevated" ( powershell -Command "Start-Process cmd -ArgumentList '/c \"%batchPath%\" elevated %batchArgs%' -Verb RunAs" exit /b ) :: Commands to run echo Running as admin... echo Command 1: dir C:\ dir C:\ echo Command 2: ipconfig ipconfig 
      • This script uses PowerShell to request admin privileges and runs the commands.
  2. "Batch file with UAC prompt to run commands as admin"

    • Description: A batch file that prompts the User Account Control (UAC) dialog to run as an admin.
    • Code:
      @echo off :: Check for admin privileges net session >nul 2>&1 if %errorLevel% neq 0 ( echo Requesting admin privileges... powershell -Command "Start-Process cmd -ArgumentList '/c \"%~f0\" %*' -Verb RunAs" exit /b ) :: Commands to run echo Running with admin rights... echo Command 1: echo Hello, World! echo Hello, World! echo Command 2: tree C:\ tree C:\ 
      • This script checks if it��s running as admin and uses PowerShell to re-run the batch file with admin privileges if not.
  3. "Batch file to run commands with admin rights using runas"

    • Description: A batch file that uses the runas command to execute commands with elevated privileges.
    • Code:
      @echo off :: Request admin privileges if "%1"=="admin" goto runCommands echo Set "runas" to elevate privileges powershell -Command "Start-Process cmd -ArgumentList '/c \"%~f0\" admin %*' -Verb RunAs" exit /b :runCommands :: Commands to run echo Running as administrator... echo Command 1: cls cls echo Command 2: systeminfo systeminfo 
      • Uses runas through PowerShell to elevate the command prompt for running the batch file.
  4. "How to create a batch file that runs as admin and executes multiple commands"

    • Description: A batch file that prompts for admin access and runs multiple commands sequentially.
    • Code:
      @echo off :: Check for elevation net session >nul 2>&1 if %errorLevel% neq 0 ( echo Requesting admin access... powershell -Command "Start-Process cmd -ArgumentList '/c \"%~f0\" %*' -Verb RunAs" exit /b ) :: Commands to execute echo Running as administrator... echo Command 1: diskpart /s script.txt diskpart /s script.txt echo Command 2: chkdsk C: /f chkdsk C: /f 
      • Checks for elevation and re-runs the batch file with admin rights if necessary.
  5. "Batch script to run CMD as admin and execute commands"

    • Description: A script to launch the command prompt as admin and run specified commands.
    • Code:
      @echo off :: Ensure this script is running as admin set "params=%*" if not "%1"=="admin" ( powershell -Command "Start-Process cmd -ArgumentList '/c \"%~f0\" admin %params%' -Verb RunAs" exit /b ) :: Commands to execute echo Running as administrator... echo Command 1: echo Running system maintenance... echo Running system maintenance... echo Command 2: sfc /scannow sfc /scannow 
      • Uses powershell to relaunch the script with admin rights if not already elevated.
  6. "Batch file with admin rights to execute commands and log output"

    • Description: A batch file that runs with admin rights and logs output to a file.
    • Code:
      @echo off :: Check for admin rights net session >nul 2>&1 if %errorLevel% neq 0 ( echo Requesting admin rights... powershell -Command "Start-Process cmd -ArgumentList '/c \"%~f0\" %*' -Verb RunAs" exit /b ) :: Commands to run and log echo Running as admin and logging output... ( echo Command 1: systeminfo systeminfo echo Command 2: dir C:\ > dir_output.txt dir C:\ > dir_output.txt ) > admin_output.log 
      • Logs the output of the commands to admin_output.log.
  7. "Batch file to prompt for admin rights and execute commands"

    • Description: A batch file that prompts for admin rights and executes commands.
    • Code:
      @echo off :: Request admin rights if "%1"=="admin" goto execute echo Requesting admin privileges... powershell -Command "Start-Process cmd -ArgumentList '/c \"%~f0\" admin %*' -Verb RunAs" exit /b :execute :: Commands to execute echo Running with admin rights... echo Command 1: netstat -an netstat -an echo Command 2: ipconfig /all ipconfig /all 
      • Checks if the script is running as admin and uses PowerShell to request admin rights if not.
  8. "Run batch file with admin rights and execute multiple commands"

    • Description: A batch file that requests admin rights and runs multiple commands.
    • Code:
      @echo off :: Elevate script if not running as admin setlocal set "batchPath=%~f0" if not "%1"=="elevated" ( powershell -Command "Start-Process cmd -ArgumentList '/c \"%batchPath%\" elevated %*' -Verb RunAs" exit /b ) :: Commands to execute echo Running as administrator... echo Command 1: dism /online /cleanup-image /restorehealth dism /online /cleanup-image /restorehealth echo Command 2: tasklist tasklist 
      • Elevates the script using PowerShell and runs multiple commands.
  9. "Batch script to run cmd with admin privileges and execute commands"

    • Description: A script to run commands with administrative privileges using a batch file.
    • Code:
      @echo off :: Check for admin rights set "params=%*" if not "%1"=="admin" ( powershell -Command "Start-Process cmd -ArgumentList '/c \"%~f0\" admin %params%' -Verb RunAs" exit /b ) :: Commands to run echo Running as admin... echo Command 1: chkdsk /f chkdsk /f echo Command 2: cleanmgr cleanmgr 
      • Uses PowerShell to re-run the batch file with admin privileges.
  10. "Create batch file to run as admin and execute commands with delays"

    • Description: A batch file that runs with admin rights and includes delays between commands.
    • Code:
      @echo off :: Request admin rights if not "%1"=="elevated" ( powershell -Command "Start-Process cmd -ArgumentList '/c \"%~f0\" elevated %*' -Verb RunAs" exit /b ) :: Commands with delays echo Running as administrator... echo Command 1: ipconfig ipconfig timeout /t 5 echo Command 2: systeminfo systeminfo 
      • Includes timeout to add delays between commands while running as admin.

More Tags

my.cnf ojdbc amazon-redshift-spectrum casing seed ormlite persistent-volumes bots paperclip android-datepicker

More Programming Questions

More Genetics Calculators

More Mortgage and Real Estate Calculators

More Housing Building Calculators

More Statistics Calculators