This article shows how to run the PowerShell script by creating a Custom Action in the Wix project and executing it during the installation.
Table of Contents
Define Prerequisite for Powershell
Wix checks if PowerShell exists in registry defined path.
Condition tag will define pre-requisite conditions, and installation stops if not met.
<Property Id="POWERSHELLEXE">
<RegistrySearch Id="POWERSHELLEXE"
Type="raw"
Root="HKLM"
Key="SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"
Name="Path" />
</Property>
<Condition Message="This application requires Windows PowerShell.">
<![CDATA[Installed OR POWERSHELLEXE]]>
</Condition>
Add PS script to File Component
If PowerShell script is used, add the file to a Component of the Directory structure. If Powershell Cmdlet is to be run, this step is not needed.
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLDIR" Name="SampleWixApp">
<Component Id="SimplePS" Guid="{29a918f6-ea9e-419a-9aab-e4f94daf3b49}" >
<File Id="SimplePS.ps1" Name="SamplePS.ps1" Source="..\files\SimplePS.ps1"/>
</Component>
</Directory>
</Directory>
</Directory>
Remember that always add ComponentRef to the Feature section for newly added Component.
Define Custom Action
Sample 1:
Add CustomAction using the property ‘POWEREHLLEXE’ and ‘INSTALLDIR’.
<CustomAction
Id='SimplePSRun'
Directory='INSTALLDIR'
ExeCommand='"[POWERSHELLEXE]" -Version 3.0 -NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -f .\SimplePS.ps1' Execute="immediate" Impersonate="no" Return='ignore'/>
Sample 2:
This is to run Custom Action in Silent Mode. For this case, WixUtilExtension.dll is to be added to the project reference.
Property can usually be used by pairing with CustomAction in Silent Execution. In this example, as [POWERSHELL] is already defined as a Property, ‘SetProperty’ is the way.
<SetProperty Id="SimplePSRun"
Before ="SimplePSRun"
Sequence="execute"
Value=""[POWERSHELLEXE]" -Version 3.0 -NoProfile -NonInteractive -InputFormat None -ExecutionPolicy Bypass -f "[INSTALLDIR]SimplePS.ps1"" />
<CustomAction Id="SimplePSRun" BinaryKey="WixCA" DllEntry="WixQuietExec" Execute="deferred" Return="ignore" Impersonate="no" />
Add the CustomAction into InstallExecuteSequence
The execution of the custom action was added to InstallExecuteSequence with Tag, Custom.
<InstallExecuteSequence>
<Custom Action="SimplePSRun" Before="InstallFinalize">NOT Installed</Custom>
</InstallExecuteSequence>
Build the wxs
msbuild simple.wixproj /t:Build /p:Configuration=Release
Please check other articles on how to create Wix Project in Visual Studio or on how to add C# Custom Action to Wix.