Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a .bat file to run an exe as administrator

I've created a batch file for running a setup.exe (code below) but I'm having issues getting the setup.exe to "run as administrator". I used this guide (shortcut trick) for aid.

start "" %CD%\Setup.exe

NOTE: My files will eventually be burnt to a DVD disk. They are currently in the directory "C:\Drivers\win8.1_x64\01a.chipset".

The batch file code runs the setup.exe (without admin privileges) fine, when running the batch file by itself (i.e. not running the shortcut).

However, when I run the batch file via the shortcut, Windows gives the error "Windows cannot find 'C:\Windows\system32\Setup.exe'".

The setup.exe directory is not in the system32 folder. Why does running the batch file find the setup.exe fine but not when I run it by the shortcut (so I can run the setup.exe as an admin)?

like image 534
Peter Avatar asked Mar 10 '23 14:03

Peter


2 Answers

You can either set the working directory in the shortcut itself, or run Setup.exe not from working directory (%CD%) but from directory where script is located:

start "" "%~dp0\Setup.exe"

Difference from earlier answer is there's no need to cd to %~dp0. Just run the setup with full path.

like image 147
Dialecticus Avatar answered Mar 13 '23 08:03

Dialecticus


By default admin privileged scripts are starting in C:\Windows\system32\

Try to put cd /d "%~dp0" at the beginning of your script which should change the work directory to the script's one.You can check this if you want to create a shortcut with admin permissions from the command line.

like image 27
npocmaka Avatar answered Mar 13 '23 07:03

npocmaka