Start Batch File As Administrator

Posted : admin On 1/26/2022

I have tried right clicking the batch file and clicking run as administrator, but all that happens is a command prompt pops up and disappears without running anything. I have added echo, pause, etc, to see if anything is running, and even made a batch file to open notepad, with the same result (nothing happens when run as administrator). In File explorer I have a library called 'Start up'. This contains 2 paths: Path 1 C: ProgramData Microsoft Windows Start Menu Programs Startup and Path 2 C: Have a.bat file start up as 'run as administrator' in start up. T PowerShell, Windows This simple batch file will enable a PowerShell script file (.ps1) to execute with Administrator permissions in Windows. When preparing PowerShell code for others to use, it’s a lot easier to wrap it up as a PowerShell script file (.ps1) and then execute it from a batch file (.bat). If you wish to Run a batch file as an Administrator one time, simply right-click on the batch file that you wish to run and select Run as Administrator. It will start executing that batch file with. See more results.

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

Batch File Commands

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).

Admin
  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' (

Start Batch File As Administrator

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'

Batch File To Run Exe

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

Run Batch File As Administrator Without Prompt

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

)

Creating Batch File In Xp

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