Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run MsiExec from PowerShell and get Return Code

With BAT/CMD script I can simply use "msiexec /i <whatever.msi> /quiet /norestart" and then check %errorlevel% for the result.

With VBScript, using the Wscript.Shell object Run() method, I can get the result like this:

"result = oShell.Run("msiexec /i ...", 1, True)"

How can I do this with PowerShell?

like image 582
Skatterbrainz Avatar asked Nov 08 '10 13:11

Skatterbrainz


People also ask

Can I run MsiExec from PowerShell?

To install the MSI file with PowerShell, we can use cmdlet Start-Process. Let say we want to install the 7ZIP MSI file on the local computer and we have downloaded and stored the source file on the C:\temp location. Once we run the below command it will start the MSI installation.


2 Answers

I would wrap that up in Start-Process and use the ExitCode property of the resulting process object. For example

(Start-Process -FilePath "msiexec.exe" -ArgumentList "<<whatever>>" -Wait -Passthru).ExitCode
like image 192
ravikanth Avatar answered Oct 13 '22 01:10

ravikanth


$LastExitCode

or

$?

depending on what you're after. The former is an integer, the latter just a boolean. Furthermore, $LastExitCode is only populated for native programs being run, while $? generally tells whether the last command run was successful or not – so it will also be set for cmdlets.

PS Home:\> cmd /c "echo foo"; $?,$LASTEXITCODE
foo
True
0
PS Home:\> cmd /c "ech foo"; $?,$LASTEXITCODE
'ech' is not recognized as an internal or external command,
operable program or batch file.
False
1
like image 31
Joey Avatar answered Oct 12 '22 23:10

Joey