Run Batch File With Admin Rights



Runas is a very useful command on Windows OS. This command enables one to run a command in the context of another user account. One example scenario where this could be useful is: Suppose you have both a normal user account and an administrator account on a computer and currently you are logged in as normal user account. Now you want to install some software on the computer, but as you do not have admin privileges you can’t install the same from the current account. One option is to switch user and login as administrator. Instead, you can do the same by simply using runas command. You just need to launch the installer from command prompt using runas command and by providing administrator login id and password.

  1. Run a batch file as administrator. To run a batch file as administrator of the computer, you need to mention the path of the batch file in the place of command in the runas syntax. For example, to run the batch file located at c: data mybatchfile.bat, you need to run the below command. Runas /user:administrator C: data mybatchfile.bat.
  2. Run Batch file with Admin Rights Unsolved:( I have a package with two programs in it, the first program runs a batch file to uninstall some old programs and the second program installs the desired program.
  3. This a trick that i used if anyone wants they can try this in batch file.This will give you the admin prompt when you run the batch file @echo off cd && cd windows/system32 && command which needs admin credentials pause.

The batch file comes to help you in this situation where you can schedule that or to configure on GPO. In this way, you war automate the creation of network drive on your network devices. Let’s check how to create a batch file to map network drive with username and password.

Let’s see the syntax of runas command with some examples.

Run a program from another user account

The command to launch a program using another user credentials is given below.

For example, if you want to open registry editor as administrator of the computer, the command would be as below.

After running the above command, you will be asked to enter the password of administrator account. After password validation, registry editor will be opened with the administrator account credentials.

To specify arguments to the program:

If you need to provide arguments to the program that need to be invoked as another user, you can put the program name and the parameters in double quotes.

For example to open the file C:boot.ini as administrator, the command would be:

Running command prompt as another user :

If you have multiple commands need to be executed with administrator(or any other user )credentials, instead of running each command using runas, you can open command prompt window once as the administrator and then run all the commands in that window. Below is the command for opening a command window using runas.

Example:

It will launch new command window after printing the above message.

Run a batch file as administrator

To run a batch file as administrator of the computer, you need to mention the path of the batch file in the place of command in the runas syntax.

For example, to run the batch file located at c:datamybatchfile.bat, you need to run the below command.

Some questions regarding runas command:

When I use runas command, I am getting the error ‘This program is blocked by group policy. For more information, contact your system administrator’. How can I fix this?

Administrator of your system might have disabled users to login from command prompt. In group policy editor, this setting can be found in the below node.

Computer Configuration -> Windows settings -> Security settings ->Local Policies -> User rights assignment

In the above path, look for the setting ‘Deny logon as a batch job‘. If you have administrator privileges, you can disable this settings. Otherwise, you need to contact the system/domain administrator.

Windows 10 >

I ran into this problem when working with symlinks on Windows 8.1 and then Windows 10. See Windows 10 symlinks.

The solution is pretty simple and it was tested and works on Windows 8.1 and Windows 10.

Note that scripts like this will eventually find their way somewhere into my git repository: https://github.com/spiralofhope/shell-random/tree/master

  • 1A batch file learning if it is run as administrator
  • 3BatchGotAdmin (Windows 8)

With thanks to https://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator/12264592#12264592

Run a batch file only if administrator ∞

Run a batch file only if not administrator ∞

An example of use can be found on my github:

Put this code more-or-less at the beginning of your batch file:

Spoiler
  • On Windows 10, as of 2016-01-31 this worked, but as of 2016-02-11 this no longer works.
  • I have not re-tested this code on Windows 8. It worked when I used it, some time ago.

    • Perhaps the change to Windows 10 also means this no longer works on Windows 8. I don't know.
  • I am told that cacls.exe is is deprecated in Windows 7 and newer, and changing calcs to icalcs works.

    • However, I've only ever used this as cacls.exe.
    • Perhaps this breaking in Windows 10 as of 2016-02-11 is because calcs.exe was removed.

Put this code more-or-less at the beginning of your batch file:

Spoiler

An example script to create a directory symlink ∞

Problem:

I want to have an application's user data (configuration) in a place of my choosing.

This example happens to be for Path of Exile - (2013 game).

  1. Create the directory C:Path_of_Exile
  2. Create the directory C:Path_of_Exile_user data
  3. Create the file C:Path_of_Exilefilename.cmd with the below content:

example filename.cmd

TODO - Your source path can't have spaces in it. I don't know why.

An example script to create many symlinks ∞

Problem:

Given a directory which has many files and subdirectories, create symlinks in a companion directory.

  1. Create the directory C:source
  2. Create the directory C:sourceone
  3. Create the directory C:sourcetwo
  4. Create the directory C:target
  5. Create the file C:sourcefilename.cmd with the below content:

example filename.cmd

@ECHO OFF

SET 'SOURCE=C:source'
SET 'TARGET=C:target'

:: BatchGotAdmin
:: https://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file
:: https://sites.google.com/site/eneerge/scripts/batchgotadmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 '%SYSTEMROOT%system32cacls.exe' '%SYSTEMROOT%system32configsystem'

Run Batch File With Admin Rights

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'

:--------------------------------------

Rights

:: Directories
FOR /D %%i in ( *.* ) DO (

How To Run Batch File With Administrator Privileges In Task Scheduler

ECHO * Processing %SOURCE%%%i
ECHO %TARGET%%%i
mklink /J '%TARGET%%%i' '%SOURCE%%%i'

)
:: Files
FOR %%i in ( * ) DO (

Run .bat File With Administrator Rights

ECHO * Processing %SOURCE%%%i
ECHO %TARGET%%%i
mklink '%TARGET%%%i' '%SOURCE%%%i'

)

Run A Batch File With Admin Rights

This works at the commandline (when run as admin!) but not from explorer.exe if I run a filename.cmd script with this: