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

Written by Alex Marin · August 6th, 2020 · 4min read

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.

Inspect environment 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 environment variables that lead to executables can be defined within it, and in MSIX we must understand that these are called App Execution Aliases.

In a MSIX package, the environment variables are available only in the container, thus the environment variables created by an MSIX package cannot be accessed outside the container.

Instead, if you need to access a file outside the container, the MSIX brings the AppExecutionAlias.

AppExecutionAlias is a feature introduced in Windows 10 that allows applications to define custom execution aliases that can be used to launch the application from the command prompt or other applications.

An AppExecutionAlias is essentially a mapping between a command-line alias and an application executable. When a user types the alias in the command prompt or specifies it as a parameter to another application, Windows automatically launches the associated application.

For example, suppose an application called "MyApp" defines an AppExecutionAlias "myalias". When a user types "myalias" in the command prompt or specifies it as a parameter to another application, Windows launches "MyApp". The AppExecutionAlias can also include parameters that are passed to the application when it is launched.

AppExecutionAlias can be used if you want to globally access the executable outside of the MSIX container.

If you want to use an environment variable that leads to a path you can have a look over our article here, but keep in mind that those variables will only be available inside the container and will not be globally accessible by other applications.

Throughout this article, we will learn more about AppExecutionAlias 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 AppExecutionAlias

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

As mentioned, using an AppExecutionAlias 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:

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

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 AppExecutionAlias 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

Add app declaration

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.

Change alias

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.

Access environment variable

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.

Video Tutorial - Environment Variables in MSIX

Subscribe to Our Newsletter

Sign up for free and be the first to receive the latest news, videos, exclusive How-Tos, and guides from Advanced Installer.

Comments: