Automate MSI Installations with PowerShell

The 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)" } }