Can you have Environment Variables in MSIX? Yes, and here's how to set them.

Written by Alex Marin · August 6th, 2020

Environment variables are settings or paths that are set outside of a program and can be globally accessed by any software.

A quick way to know the environment variables present on a machine is to open the CMD and type set. This step yields all the machine variables, as well as the current user variables.

An example of a popular third-party environment variable is the one set by Java. With the environment variable present on the machine, applications that have a Java dependency can easily access the required resources to function, even without knowing the location of the Java installation.

With MSI, you can create an environment variable by using the Environment table, or by navigating to the Environment Variables Page in Advanced Installer.

Be aware that environment variables can be separated into two types:

  1. Environment variables that lead to folder paths (i.e. Java installation directory)
  2. Environment variables that lead to executables

This is a bit different with MSIX since only the second type of variable can be defined within it.

Throughout this article, we will learn more about environment variables in MSIX and how to create one using Advanced Installer.

If you’re new to Advanced Installer, we recommend you take advantage of the 30-Day Full-Featured Free Trial (no credit card required).
Start free trial

MSIX Environment Variables

As specified in our MSIX introduction article, an MSIX package behavior is determined by the AppxManifest.xml.

In MSIX, environment variables are redefined as AppExecutionAlias, and declared as extensions for an application.

However, using an AppExecutionAlias means that only environment variables leading to executables can be created with MSIX. It is not possible to reference a path to a directory.

Let's illustrate this with an example for HelloWorld. To create an AppExecutionAlias for a HelloWorld executable present in the MSIX package, you need to add the following line to AppxManifest:

<Application EntryPoint="Windows.FullTrustApplication" Executable="AI_STUBS\AiStub.exe" Id="EnvironmentVariables">
<uap5:Extension Category="windows.appExecutionAlias" EntryPoint="Windows.FullTrustApplication" Executable="HelloWorld.exe">
<uap5:ExecutionAlias Alias="HelloWorld.exe" />

Notice how the AppExecutionAlias is present under an Application element in the XML. Without defining an application in the MSIX package, an AppExecutionAlias cannot be created.

MSIX Environment Variables with Advanced Installer

With its powerful GUI, Advanced Installer makes it easier to declare an AppExecutionAlias in your MSIX package without worrying about manifest alterations.

To add an AppExecutionAlias:

1. Navigate to the Declarations Page.

2. Under Application Declarations, right-click your application and select Add Application Declaration > App Alias

3. Advanced Installer will automatically populate with the executable from the application. If required, you can manually select a different executable.

4. Change the Alias if needed. The Alias represents how the application will be called by other programs.

For this demo purpose, we created an environment variable that points to HelloWorld.exe.

Once the package is signed, built, and installed, you can open a cmd window and access HelloWorld.exe globally in the system.

I hope you found this useful!

What other MSIX related topics would you like us to explore and create a tutorial article for? Let us know in the comments.