28

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?

serenesat
  • 4,611
  • 10
  • 37
  • 53
Skatterbrainz
  • 1,077
  • 5
  • 23
  • 31

3 Answers3

51

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
ravikanth
  • 24,922
  • 4
  • 60
  • 60
  • That's what I was looking for! However, it's "ArgumentList" not "ArgumentsList", but in any case you answered the question - thank you! – Skatterbrainz Nov 12 '10 at 13:24
  • 3
    Notes; 1.Inside the quoted -ArgumentList, arguments are space-delimited and single quotes can be used to hold items like a path with spaces in the name. 2.ExitCode only becomes available if all spawned windows are closed; 3.I'm on PS v4.0. – AnneTheAgile Jan 21 '15 at 18:23
  • 1
    If you can't use the quoted `-ArgumentList " '$arg1\ with spaces\' arg2"` you can comma separate multiple arguments `-ArgumentList "$arg1\ with spaces\", arg2`, once it stops finding commas PowerShell assumes the next is another positional argument or if something like `-Wait` it will just resume processing the named arguments. – dragon788 Nov 17 '16 at 20:18
17
$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
Joey
  • 344,408
  • 85
  • 689
  • 683
0

You can also use the powershell app deployment kit which provides several things.

Then you can use for example

Execute-MSI -Action 'Install' -Path "$dirFiles\your.msi" -AddParameters "INSTALLFOLDER=C:\$appFolder"

info http://psappdeploytoolkit.com/

Johan A.
  • 378
  • 3
  • 7