How do I run a Windows installer and get a succeed/fail value in PowerShell?
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 -PassThruif($p.ExitCode -ne 0){ throw "Installation process returned error code: $($p.ExitCode)"}
Depends. MSIs can be installed using WMI. For exes and other methods, you can use Start-Process and check the Process ExitCode.
msi's can also be installed using msiexec.exe, msu's can be installed using wusa.exe, both have a /quiet
switch, /norestart
and /forcerestart
switches and a /log
option for logging (specify the file name).
You can read more about the options if you call them with /?
Note: wusa fails silently when they fail, so you have to check the log file or eventlog to determine success.