How to access Session object from PowerShell custom actions?Copy link to this sectionLink to this section copied!

When creating PowerShell custom actions, you may need access to installer properties, feature and component states, logging capabilities, or other Windows Installer data. This can be done through the Windows Installer Session object, which provides access to the installation database and installation context.

The Session object is automatically available in PowerShell custom actions and can be used to query installation data, update installer properties, change feature or component states, and write messages to the installation log.

NoteThis functionality was added starting with version 23.8 of Advanced Installer. PowerShell support in Advanced Installer is designed to replace legacy VBScript custom actions. PowerShell custom actions can access the same Windows Installer Session data while providing a modern and more powerful scripting environment.

NoteThe AI_GetMsiProperty and AI_SetMsiProperty functions remain available for backward compatibility. Internally, they use the Windows Installer Session object to access and modify installer properties.

1. Write messages to the installation logCopy link to this sectionLink to this section copied!

Use the Session object to write informational messages to the MSI log. This is useful for troubleshooting and debugging PowerShell custom actions.

$MSI_MESSAGE_INFO = 0x04000000

$productVersion = $Session.Property["ProductVersion"]

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Detected product version: $productVersion"
)

NoteThe Session.Message() method writes messages directly to the Windows Installer log. Messages sent using $MSI_MESSAGE_INFO are informational and are not displayed to the user.

2. Access installer propertiesCopy link to this sectionLink to this section copied!

Use the Session object to read and update installer properties.

$MSI_MESSAGE_INFO = 0x04000000

$prodName = $Session.Property["ProductName"]
$prodVer  = $Session.Property["ProductVersion"]

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Product name: $prodName"
)

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Product version: $prodVer"
)

# Set APPDIR
# Must be scheduled after CostInitialize
$Session.Property["APPDIR"] = "C:\Test\$prodVer\"

# Installer language
$lang = $Session.Language

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Language: $lang"
)

ImportantProperties modified through Session.Property become available after the custom action executes. Make sure the custom action is scheduled before any actions that depend on the updated property value.

3. Access feature stateCopy link to this sectionLink to this section copied!

Retrieve or modify feature installation states from a PowerShell custom action.

$MSI_MESSAGE_INFO = 0x04000000

# Replace "MainFeature" with an actual feature name
$featureName = "MainFeature"

$currentState = $Session.FeatureCurrentState[$featureName]
$requestState = $Session.FeatureRequestState[$featureName]

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Feature '$featureName': current=$currentState, requested=$requestState"
)

# Request feature removal (Absent = 2)
$Session.FeatureRequestState[$featureName] = 2

$requestState = $Session.FeatureRequestState[$featureName]

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Feature '$featureName' requested state updated: $requestState"
)

NoteCommon Windows Installer feature states are: -1 (Unknown), 2 (Absent), and 3 (Local).

4. Access component stateCopy link to this sectionLink to this section copied!

Retrieve or modify component installation states from a PowerShell custom action.

$MSI_MESSAGE_INFO = 0x04000000

# Replace with an actual component name
$compName = "component_name"

$compCurrent = $Session.ComponentCurrentState[$compName]
$compRequest = $Session.ComponentRequestState[$compName]

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Component '$compName': current=$compCurrent, requested=$compRequest"
)

# Request local installation (Local = 3)
$Session.SetComponentState($compName, 3)

$compRequest = $Session.ComponentRequestState[$compName]

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Component '$compName' requested state updated: $compRequest"
)

NoteChanging a component state updates the requested state for the current installation session. Windows Installer evaluates and applies the change later in the installation process.

5. Determine installation modeCopy link to this sectionLink to this section copied!

Use installer properties to determine whether the package is being installed, modified, repaired, or removed.

$MSI_MESSAGE_INFO = 0x04000000

$installed = $Session.Property["Installed"]
$remove = $Session.Property["REMOVE"]

if ($remove -eq "ALL")
{
    $Session.Message(
        $MSI_MESSAGE_INFO,
        "Uninstall operation detected"
    )
}
elseif ($installed)
{
    $Session.Message(
        $MSI_MESSAGE_INFO,
        "Maintenance operation detected"
    )
}
else
{
    $Session.Message(
        $MSI_MESSAGE_INFO,
        "First-time installation detected"
    )
}

6. Retrieve feature and component costsCopy link to this sectionLink to this section copied!

Use the Session object to retrieve disk space requirements for features and components.

ImportantFeature and component costs become available only after the InstallValidate standard action has completed.

$MSI_MESSAGE_INFO = 0x04000000

# Execute after InstallValidate

$costs = $Session.ComponentCosts("", 3)

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Component count: $($costs.Count)"
)

foreach ($record in $costs)
{
    $drive = $record.Drive
    $finalCost = $record.Cost
    $tempCost = $record.TempCost

    $Session.Message(
        $MSI_MESSAGE_INFO,
        "Drive=$drive Cost=$finalCost TempCost=$tempCost"
    )
}

$featureCost = $Session.FeatureCost("MainFeature", 0, 3)

$Session.Message(
    $MSI_MESSAGE_INFO,
    "Feature cost: $featureCost"
)

Session.FeatureCost() requires three parameters:

  • Feature (the feature name)
  • CostTree (which related features are included in the calculation)
  • State (the target installation state)

Supported CostTree values:

  • 0 - Feature only
  • 1 - Child features
  • 2 - Parent features
  • 3 - Entire product

Common State values:

  • 2 - Absent (removal cost)
  • 3 - Local (installation cost)