How to launch a CMD or BAT file
The following article uses options that are available starting with the Professional edition and project type.
Windows Installer cannot launch batch files directly by using installed or attached custom actions. However, in Advanced Installer you can run a CMD or BAT file through a Launch file predefined custom action. In the case of a 64-bit CMD or batch file you can just enable the option Use 64-bit Launcher.
Since the files cannot be attached to the MSI, they need to be present on the target machine when the custom action runs. This can be done in two ways:
- Add the CMD or BAT file in the Files and Folders page. This way the file will be installed by the package, and it can be launched after the Install Execution Stage -> Add Resources action group. In this case, the custom action should be set as "Deferred" in the Custom Action Properties page to make sure it can access the installed file.
- Another approach is to add the CMD or BAT file as a temporary file. This way, the file is copied on the target machine after the Wizard Dialogs Stage -> Paths Resolution -> CostFinalize standard action and removed after Install Execution Stage -> Finish Execution -> InstallFinalize. In this case the custom action can use any execution option.
After the file is added in the "Files and Folders" page, you can configure the custom action.
The File to launch field should contain the full path of the file you want to run. This field is of Formatted Type and thus it can include references to files, folders and properties. For example, if the file is called "MyBatchFile.bat" and it is added as a regular file, then you can add a reference to it in the "File to launch" field like this:
For a temporary file, the "File to launch" field would look like this:
Pass parameters to the .bat file:
To pass parameters to your BAT file, you can add them in the Command Line field from the custom actions properties, something like this:
If a property value contains spaces it must be enclosed in quotation marks.
In the .bat file access the command line parameters using %1, %2, etc. E.g:
echo %1 %2 pause
The "Condition" field in the Custom Action Properties page should contain the custom action's condition. Edit this field using Smart Condition Edit Control.
A custom action that doesn't have a condition will run in every installation
process: first-time install, uninstall, repair, modification.
It is recommended to be very specific when setting the condition to execute the BAT. For example, the installation can get broken if the "Uninstall" option is left enabled and the BAT is not meant to run on uninstall.