Run Batch File As Administrator

Posted : admin On 1/25/2022

The batch file doesnt run as admin and based on my research it can't auto run as admin. My idea is to move the program install batch script to a separate folder and create a new batch script for the startup folder that will force the original batch file to run as administrator. Running.BAT or.CMD files in minimized mode. To run a batch file in a minimized window state, follow these steps: Create a shortcut to the.BAT or.CMD file. To do so, right click on the file, click Send To, Desktop (create shortcut) Right click on the shortcut and choose Properties; In the Run: drop down, choose Minimized; Click OK. Run batch file as administrator Launch a batch file with elevated privileges from an administrator account. Create a shortcut of the batch file, select in properties of the shortcut - advanced - run as administrator. Start a batch file as administrator from a user account. Choose the easiest way and use RunAsRob https://runasrob.com. So far it seems the best way would be to make a batch file to run the program as a different user and then set the keyboard shortcut to that batch command. However, I cannot seem to write a successful batch command. We are using Task Scheduler to run a batch file by creating a scheduled task configured to 'Run As Administrator'. From the scheduled task properties we set the following security options: 1) Run whether user is logged on or not 2) Run with highest privileges.

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
File

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 )

Run scheduled task batch file as administratorAdministrator

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

Run Scheduled Task Batch File As Administrator

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

Run Batch File As Administrator Automatically

)

Run Batch File As Administrator Automatically

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