How do I run a Windows installer and get a succeed/fail value in PowerShell?

powershell, windows-installer

Solution

These answers all seem either overly complicated or not complete enough. Running an installer in the PowerShell console has a few problems. An MSI is run in the Windows subsystem, so you can't just invoke them (`Invoke-Expression` or `&`). Some people claim to get those commands to work by piping to `Out-Null` or `Out-Host`, but I have not observed that to work.

The method that works for me is `Start-Process` with the silent installation parameters to `msiexec`.

$list = 
@(
    "/I `"$msi`"",                     # Install this MSI
    "/QN",                             # Quietly, without a UI
    "/L*V `"$ENV:TEMP\$name.log`""     # Verbose output to this log
)

Start-Process -FilePath "msiexec" -ArgumentList $list -Wait

You can get the exit code from the `Start-Process` command and inspect it for pass/fail values. (and here is the exit code reference)

$p = Start-Process -FilePath "msiexec" -ArgumentList $list -Wait -PassThru

if($p.ExitCode -ne 0)
{
    throw "Installation process returned error code: $($p.ExitCode)"
}

Problem

I would like to install a set of applications: .NET 4, IIS 7 PowerShell snap-ins, ASP.NET MVC 3, etc. How do I get the applications to install and return a value that determines if the installation was successful or not?

Original source