Start Batch File As Administrator

Posted on  by 



Open File Explorer and locate the folder with the saved batch file. To run the batch file as administrator on Windows 10 with the help of file explorer right-click on it. The properties show you the option to Run as Administrator. Click Yes on the confirmation dialogue box. In order to run as an Administrator, create a shortcut for the batch file. Right click the batch file and click copy Navigate to where you want the shortcut Right click the background of the directory.

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

Administrator

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'

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (

echo Requesting administrative privileges..
goto UACPrompt

) else ( goto gotAdmin )

Batch

: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'

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

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

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

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

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

)

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

To perform various actions on Windows 10, you can simply use batch files with Command Prompt. While writing commands on your system for DOS or Windows, you need to retype a lot of them in order to run commands. Batch files store the commands in a serial so as to run and executed in the same line-up. The command-line interpreter takes the input from the batch file and executes it in the same order. While there are many ways to run the batch file as Administrator on Windows 10, we discuss them in this article. Let’s begin with an overview of batch files.

What Is A Batch File?

Batch file is a script file which consists of a series of commands in plain text for executing in the command line interpreter. It was required to make the work easy for users to save the often-used commands. When the batch file is run, the commands are executed line by line. Batch file is in the form of .bat,.cmd,.btm file extensions. You can find it saved in the simple text form on the Notepad or another text editor.

Simple commands for batch files will be:

echo- Prints input string.

cls- Clears command Window.

pause- Stop the batch file execution.

title- Changes the prompt window title.

exit- Exit the command prompt.

Start Batch File As AdministratorStart Batch File As Administrator

copy- Copies a file/files.

::- Add a comment in the batch file.

There are different types of batch files available.

  1. INI – Initialization file is the set of default variables for Windows programs. It is denoted by *.ini
  2. SYS – System files are denoted by *.sys and it may be modified.
  3. COM – Commands files are denoted by *.com and are the executable files for DOS.
  4. CFG – Configuration files are denoted by *cfg.
  5. CMD – Used in Windows New Technology operating systems (32 bit).

How to create a batch file?

To proceed, all you need is any text editor on your system with knowledge of command prompt. Follow the steps below to create a batch file. In the following method, we make use of a notepad to create a batch file.

Step 1: Open the Start Menu and type Notepad in the search bar.

Step 2: Open Notepad.

Step 3: Type the following –

@ECHO OFF

ECHO Your first Batch File.

PAUSE.

Here we are trying to run a batch file with the result which says – Your first batch file.

Step 4: Click on File from the menu, and click on Save from the drop-down options.

Step 5: Save the file name with .bat file extension name. For eg: First_batch_file.bat

Run Batch File As Administrator On Windows 10

There are many ways to run batch file as administrator on Windows 10. It can be executed by file explorer, command prompt and task scheduler.

1. Run batch file as administrator on Windows 10 using File explorer-

This is one of the methods to run batch file on demand. Open File Explorer and locate the folder with the saved batch file. To run the batch file as administrator on Windows 10 with the help of file explorer right-click on it. The properties show you the option to Run as Administrator.

Click Yes on the confirmation dialogue box. It is one of the simplest methods to run batch file without command prompt.

2. Run batch file as administrator on Windows 10 using Command Prompt-

It is another of the method to run batch file as administrator on Windows 10 with command prompt. Follow the steps given below:

Step 1: Open the Start Menu and type command prompt on the search bar.

Step 2: Select Command Prompt and then on the right-side panel click on Run as Administrator.

Step 3: The command prompt opens with the title Administrator: Command Prompt.

Type the batch file path and name and press Enter.

It is how you can use batch file instead of typing the commands here to execute a task. To run command on command prompt by simply adding a batch file which consists of script.

3. Run batch file as administrator on Windows 10 using Task Scheduler-

Another method to run batch file as administrator on Windows 10 which will schedule a task. The following steps will make you schedule a task by a batch file.

Step 1: Open the Start menu and type Task Scheduler.

Internet explorer 11 will not open pdf. Step 2: Open Task Scheduler from the results.

Step 3: To open a new folder, right-click on the Scheduler library. Type a name for your folder and click on OK.

Step 4: On the left panel, right-click on the newly saved folder. Select the Create Basic Task.

Step 5: Give the name and description of the type of scheduled task. Click Next, and select when do you want the task to start off – Daily, weekly, monthly etc.

Start Batch File As Administrator

Step 6: Select the type of the task, and in the Start, a Program section of Action Centre, enter the batch file path from the browser option. And click on Finish.

Wrapping Up:

Start Batch File As Administrator Linux

This is how you can run batch file as administrator on Windows 10. It included the most commonly used method of using a command prompt to run batch file as administrator. Along with the task scheduler and file explorer which are other methods to run batch file without prompt on Windows 10.

Start Batch File As Administrator Windows 10

We Love To Hear From You

Please tell us your views on this post in the comments section below. Also, leave your views and queries in the comments section below. We would love to get back to you with a solution. We post regularly on the tips and tricks along with solutions to common issues related to technology. Subscribe to our newsletter to get regular updates on the tech world. Follow us on Facebook, Twitter, LinkedIn, and YouTube and share our articles.

What Do You Think?
Responses




Coments are closed