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. Runas /user:administrator C:datamybatchfile.bat. File ExplorerManageRun as Admin. Command Prompt: Use the following command line. Note that the command should have the target file in the right location. In the example below, the file is.
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:
Run Batch File As Administrator Automatically
- 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 changingcalcs
toicalcs
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.
- However, I've only ever used this as
Put this code more-or-less at the beginning of your batch file:
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).
- Create the directory
C:Path_of_Exile
- Create the directory
C:Path_of_Exile_user data
Create the file
C:Path_of_Exilefilename.cmd
with the below content:
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.
- Create the directory
C:source
- Create the directory
C:sourceone
- Create the directory
C:sourcetwo
- Create the directory
C:target
Create the file
C:sourcefilename.cmd
with the below content:
@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 )
: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:
In Microsoft Windows, when you try to run a program or perform an operation that requires Administrator privileges, a UAC prompt requires your permission to proceed.
Similarly, you may encounter a permissions error when trying to run commands in a Command Prompt window that require Administrator privileges. These errors happen because UAC blocks the Command Prompt from running Administrator commands unless you run the Command Prompt as Administrator.
UAC acts as a safeguard, requiring your explicit permission to run any software that can modify the Windows system.
Select a link below and follow the steps to run a program as administrator or disable UAC.
Running programs as Administrator
If a program requires Administrator privileges to perform certain functions, you need to run the program as Administrator.
To run a program as Administrator in Windows 10, right-click the icon in your Start menu and select Run as administrator.
For example, in the image below, we are running the Windows 10 command prompt as administrator. We'll discuss this more in the next section.
If you right-click a shortcut in your Start menu and the Run as administrator option is not listed, that function is not available for the program.
When you receive the UAC prompt confirming that you want to grant privileges, click Yes.
Running commands as Administrator
In the example above, we ran the Windows 10 command prompt as Administrator. We did this by clicking Start menu > All apps > Windows System, then right-clicking Command Prompt > Run as administrator.
Running the command prompt as Administrator allows you to run commands that change or edit your system files. For example, in the Administrator command prompt, you can successfully run the sfc command (System File Checker).
You should now be able to execute the command you were trying to run previously when you received the error message. With administrative rights activated, the command should execute successfully.
Configuring a shortcut to always run a program as Administrator
To configure most program shortcuts to always run the program as Administrator, follow the steps below.
- Locate the program shortcut, right-click the shortcut and select Properties.
If the program icon is in the Start menu, you need to right-click the icon and select Open file location. Then begin with the step above.
- On the Properties window, click the Compatibility tab.
- Check the box for Run this program as an administrator and click OK to save the shortcut settings change.
If you do not see a Compatibility tab in the Properties window, follow the steps below instead to always run the program as Administrator.
- Repeat step 1 above to access the Properties window for the program shortcut.
- On the Properties window, click the Shortcut tab.
- Click the Advanced button.
- On the Advanced Properties window, check the box for Run as administrator and click OK.
- Click OK on the main Properties window to save the shortcut settings change.
Disabling UAC
To disable the User Account Control prompt in Windows, follow the steps below for your version of Windows.
Windows 10 & Windows 8
- Open the Windows Control Panel.
- Click the User Accounts icon.
- Click the Change User Account Control settings link.
- Move the slider down to Never notify and click OK.
- Click Yes at the User Account Control prompt.
Windows 7 & Windows Vista
- Open the Windows Control Panel.
- Click the User Accounts and Family Safety icon.
- Click the User Accounts option.
- Click the User Account Control settings link.
- Move the slider down to Never notify and click OK.
- Click Yes at the User Account Control prompt.
Script Run As Admin
Additional information
- See the elevated command prompt and UAC definition for further information and related links.