Use PowerShell scripts to launch an application in user context

As a developer, you will need to extend the capabilities of your Windows Installer package from time to time. A common reason is when you need to give users the option to launch an application at the end of installation in the context of the user who runs the installer.

You can achieve this by following two main steps:

  • Checking whether the installer is running elevated or not.
  • Launching the application based on the return result from the first step.

Let’s see how this looks in practice.

1. First step: Check if the installer is running elevated or not

To check if an installer is running elevated, we can use a Custom Action. In this tutorial, we will use a PowerShell script, but this also works with VBScripting or JavaScripting.

$user = [Security.Principal.WindowsIdentity]::GetCurrent();
      (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)

This script gets the account that executed the process and then checks if that account is part of the Administrator group. If it is, then it returns a true result; if it isn't, then it will return a false result.

NoteThe corresponding Custom Action must be set to Immediate.

Powershell inline

If you prefer, you can write your script using C# or C++ and then use a “Call function from attached native DLL” Custom Action to call the function from the DLL. The DLL will be embedded in the MSI, but it will not be installed on the target device with the other application files.

NoteYou can check How to create fully fledged C# custom actions for more details on how to create, integrate, and debug a C# Custom Action.

2. Second Step: Launching the application

Once we've checked if the installer is running elevated (or not), all we need to do is launch the application based on the return result. To do this, we can use two “Launch File” Custom Actions and set a condition for each of them, as follows:

  • One Custom Action will have the “Run as admin” flag enabled and will be conditioned to run only if the user who triggered the action has administrator privileges.
  • The other Custom Action will have the “Run as admin” flag disabled and will be conditioned to run only if the user who triggered the action does not have administrator privileges.

Launch file ca ra

Launch file ca

Additionally, you can place a CheckBox so that the users can choose if they want to launch the application or not. Since we are not using the default mechanism to launch the application at the end of the installation sequence, we cannot use the default checkbox to condition the execution of our Custom Actions.

Launch app checkbox

To execute the two Custom Actions, select the “Finish” button from the “ExitDlg'' dialog and then set the corresponding conditions for the two Custom Actions using the “Published Events'' editor.

Finish published events

NoteIf you want to review how to execute a Custom Action on a PushButton, read our How to execute a custom action when a button is pushed article.

That’s it.

After following this quick tutorial, you will see that if you run the installer, the application will get launched at the end of the installation, in the context of the user who runs the installer.