How to Run EXE files as Administrator in Windows Programmatically

Written by Catalin Gheorghe · May 11th, 2023

Do you need to execute your application (exe file) as an administrator or apply compatibility options to it?

There are situations where we need to run applications in admin mode, such as when your application is actively modifying the system and requires permissions, or when a legacy application is not working correctly in a modern system because it needs compatibility adjustments.

To manually set an application to run as administrator, you can right-click your EXE --> "Properties" --> "Compatibility" tab --> "Run this program as an administrator" option (or maybe "Run this program in compatibility mode for:" option).

But what if you want to do this programmatically? In this article, we'll provide a step-by-step guide to help you code your way into admin mode.

Step 1: Finding the settings

First, it's essential to understand that any change you make to your Windows system will likely be reflected in the registries.

If you apply any setting mentioned before (“Run as Administrator” or “Run this program in compatibility mode”), these will be reflected in the following registry locations:

  • "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
  • "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
Registry locations - Run this program as an administrator

As seen in the screenshot above, when you select "Run this program as an administrator" for an executable, the "RUNASADMIN" value is added to the registry.

To do this programmatically, you can use a script to add the necessary registry value. You can use different scripting languages (e.g., PowerShell, VBS) or a .DLL/.EXE (C#). For this article, we chose to use PowerShell, and here's an example script:

# The registry key that stores the option
$runAsAdminReg = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
# The path to our .EXE application
$name = "C:\Users\Catalin\Desktop\sample.exe"
# The registry value for our option
$value = "RUNASADMIN"
# The cmdlet that adds the value
New-ItemProperty -Path $runAsAdminReg -Name $name -Value $value -PropertyType string -Force | Out-Null

Using the script above, we can also force the program to run in compatibility mode by changing the $value variable to one of the following values:

- WIN95

- WIN98

- WIN4SP5

- WIN2000

- WINXPSP2

- WINXPSP3

- VISTARTM

- VISTASP1

- VISTASP2

- WIN7RTM

- WINSRV03SP1

- WINSRV08SP1

- WIN8RTM

Step 2: Using Advanced Installer to Run EXE as Administrator

Now that we know how to make a program always run as an administrator or run in compatibility mode programmatically, let’s see how we implement it through the Advanced Installer.

You can add the PowerShell script as a "Run PowerShell Inline Script" custom action. The steps to add the script are:

1. Navigate to the Custom Actions Page.

2. Click on the "Add custom action with sequence" button that you can find on the right side of the custom action's name.

3. Schedule the custom action after the "Add Resources" action group.

4. Set the custom action execution time to "When the system is being modified (deferred)".

5. If you are unsure whether your setup will be installed on a 32-bit machine or a 64-bit machine, you could set the "Platform" to "Auto".

6. To avoid using a hard-coded path in your script, you can make use of the installer properties.

If the .EXE is added in the "Files and Folders" page, under the "Application Folder" folder, your script can look like this:

Advanced Installer - Custom Actions Page

What we are doing above is taking the path that is stored in the "APPDIR" property and adding the name of our .EXE to the path.

ImportantImportant: For the script to successfully execute, the setup must be run with administrative privileges, as they are required when writing to the registries.

7. Click Build in the upper left corner.

Conclusion

That's it! You now know how to configure any application (EXE) to always run as needed (either as an administrator or with compatibility settings). If you have any other questions or need further assistance, feel free to reach out.

Written by
See author's page
Catalin Gheorghe

Catalin Gheorghe has been working as a Solution Engineer in the Advanced Installer's Customer Support team for over two years. By helping customers understand the product and answering questions about how to address specific technical issues, he benefits from an unmatched exposure to the developer in charge of the installer or to the packager. He uses this experience to provide comprehensive, valuable, easy to understand and to put in practice pieces of advice. You can meet him on our forums or over the e-mail at support at advancedinstaller dot com . He usually writes for Advanced Installer's forum and from time to time we are happy to also have him share his knowledge on our blog.

Comments: