Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a shortcut to launch an App with admin privileges from the cmd-line?

I have an installer (Inno-Setup) that installs my application to a path defined by the user. At the end of the install routine i want to create a shortcut that starts the application with admin privileges. The solution should work on all win version from winXP to Win7.

What can i do to achieve this?

I know that it is possible with a batch script, that executes a nasty vb-script. The disadvantage is that the cmd-window popup and it only works on win7 i guess.

I also tried the command mklink to create a hyperlink, but it does not work because it is not possible to pass an argument that set the admin priviliges.

like image 836
Chriss Avatar asked Apr 18 '13 12:04

Chriss


2 Answers

You can add a registry-key that tells windows to execute your program as admin:

Under HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers, just add a key(REG_SZ) <Path to your exe> with the value RUNASADMIN. When you launch your exe, you will be prompted for admin-access.

With that, you can simply create a normal shortcut to your executable like you would do it with Inno-Setup.

If you want to do so via a cmd or a batch-file, you can use the following command:

reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "<Path to your exe>" /t REG_SZ /d RUNASADMIN
like image 185
looper Avatar answered Oct 05 '22 13:10

looper


The "Run as admin" is a property of the executable, not the shortcut. You should add the required manifest that makes Windows prompt for elevation.

To do this on Windows XP, you will need to use the runas verb with ShellExecute() to run as a different user, but this will remove any ability to access the local profile. This can be done from your exe when it finds it's not running with full admin access.

like image 37
Deanna Avatar answered Oct 05 '22 13:10

Deanna