Can one use environment variables in Inno Setup scripts?

According to this page in the Inno Setup documentation, the value of environment variables can be retrieved using the following syntax:

{%name|default}

It can be even easier:

OutputDir={#GetEnv("TEMP")}\

The syntax is different, if you want to resolve the variable on install-time or on compile-time. That's why there are two existing answers that show completely different solutions that work for some and not others. Because different readers look for different things here.


On install-time

If you need to resolve the variable on the target machine, while installing, you can use the {%NAME|DefaultValue} "constant".

[Files]
Source: "MyApp.dat"; Dest: "{%MYAPP_DATA_PATH|{app}}"

If you need to resolve the variable on the target machine in Pascal Script code, you can use GetEnv support function.

Path := GetEnv('MYAPP_DATA_PATH');

On compile-time

If you need to resolve the variable on the source machine, while compiling the installer, you can use GetEnv preprocessor function:

[Files]
Source: "MyApp.dat"; Dest: "{#GetEnv('MYAPP_DATA_PATH')}"

You can use the same syntax even in Pascal Script, though it would make sense only in very special circumstances.

Path := '{#GetEnv('MYAPP_DATA_PATH')}';

I ran into the same problem when trying to specify the source location of files in the [Files] section. I used the GetEnv function to define a new constant.

#define Qt5 GetEnv('QT5')
[Files]
Source: {#Qt5}\bin\Qt5Concurrent.dll; DestDir: {app};