-
Automate MSI Installations with PowerShell
Posted on July 16th, 2012 4 commentsThe following is a PowerShell wrapper for msiexec.exe that enables you to automate the installation of MSI packages. You will see in the code that I picked certain msiexec options that were required for the particular need I had, such as quiet install (/qn). If you need other options, simply add or remove them to the arguments section.
The function takes pipeline input, so to install an msi you can do the following
"C:\temp\GoogleChromeStandaloneEnterprise.msi" | Install-MSIFile -Verbose
If the installation fails then the exit code will be displayed if verbose mode has been selected:
I’ve also made Uninstall-MSI which requires you supply the MSI file to be able to uninstall and does not remove from Add / Remove Programs.
function Install-MSIFile { [CmdletBinding()] Param( [parameter(mandatory=$true,ValueFromPipeline=$true,ValueFromPipelinebyPropertyName=$true)] [ValidateNotNullorEmpty()] [string]$msiFile, [parameter()] [ValidateNotNullorEmpty()] [string]$targetDir ) if (!(Test-Path $msiFile)){ throw "Path to the MSI File $($msiFile) is invalid. Please supply a valid MSI file" } $arguments = @( "/i" "`"$msiFile`"" "/qn" "/norestart" ) if ($targetDir){ if (!(Test-Path $targetDir)){ throw "Path to the Installation Directory $($targetDir) is invalid. Please supply a valid installation directory" } $arguments += "INSTALLDIR=`"$targetDir`"" } Write-Verbose "Installing $msiFile....." $process = Start-Process -FilePath msiexec.exe -ArgumentList $arguments -Wait -PassThru if ($process.ExitCode -eq 0){ Write-Verbose "$msiFile has been successfully installed" } else { Write-Verbose "installer exit code $($process.ExitCode) for file $($msifile)" } }function Uninstall-MSIFile { [CmdletBinding()] Param( [parameter(mandatory=$true,ValueFromPipeline=$true,ValueFromPipelinebyPropertyName=$true)] [ValidateNotNullorEmpty()] [string]$msiFile ) if (!(Test-Path $msiFile)){ throw "Path to the MSI File $($msiFile) is invalid. Please supply a valid MSI file" } $arguments = @( "/x" "`"$msiFile`"" "/qn" "/norestart" ) Write-Verbose "Uninstalling $msiFile....." $process = Start-Process -FilePath msiexec.exe -ArgumentList $arguments -Wait -PassThru if ($process.ExitCode -eq 0){ Write-Verbose "$msiFile has been successfully uninstalled" } else { Write-Error "installer exit codeĀ $($process.ExitCode) for fileĀ $($msifile)" } }4 responses to “Automate MSI Installations with PowerShell”

-
Can we run this againts remote computer?
-
Chaitanya September 20th, 2012 at 09:04
I was not successful running uninstall with /qn through power shell. But it worked with command prompt. Any idea how can I run it through power shell?
-
santhosh October 17th, 2012 at 10:38
is that script is working?
can we run this script for remote computer installation?
Leave a reply
-











Orhan August 9th, 2012 at 13:50