Run Batch File 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. 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. Now, when I run the batch file, the command prompt is C: windows System32 cmd.exe When I run the dll using shell 32, the executable called is C: windows SysWOW64 cmd.exe. If I finally cave in and choose to launch the shortcut with 'Run as Administrator', the script executes OK - but that merely triggers the very UAC prompt I'm trying to avoid. The account I'm using is an administrator. My script, amongst other things, executes runas.exe to launch a.EXE file under a second admin account. Any bright ideas? 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.

  1. Run Batch File As Administrator Login
  2. Run Batch File As Administrator Cmd
  3. Run Batch File As Administrator Windows 10
  4. Run Batch File As Administrator Remotely

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'

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:

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'

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

echo Requesting administrative privileges...
goto UACPrompt

Run Batch File As Administrator Login

) else ( goto gotAdmin )

Run Batch File As Administrator Cmd

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

Administrator

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

Run Batch File As Administrator Windows 10

)

Run Batch File As Administrator Remotely

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