How to Install and Uninstall MSI Packages using Powershell

Written by Alex Marin · February 29th, 2024

Installing MSI packages through PowerShell is a widely known technique that simplifies the deployment of software across multiple systems, especially for system administrators and IT professionals.

In this article, I will guide you through the process of installing MSI packages using PowerShell, covering basic installations and those requiring arguments. We will also explore the PowerShell App Deployment Toolkit (PSADT), which further streamlines the installation process.

Installing MSI Packages Through PowerShell

Installing an MSI package using PowerShell is straightforward with the `Start-Process` cmdlet. This cmdlet initiates the process of installing an MSI file. For a basic installation without any arguments, use the following script:

Start-Process 'msiexec.exe' -ArgumentList '/I "Path\To\Your\Package.msi" /qn' -Wait

In this script, `msiexec.exe` is the command-line utility for installing, modifying, and managing Windows Installer packages.

The `/I` switch indicates that you want to install the specified package. The path to the MSI file should be provided in place of "Path\To\Your\Package.msi". The `/qn` argument sets the user interface level to no UI, making the installation process run silently.

For installations requiring specific arguments, such as logging and user interface options, you can modify the script accordingly. For example, to install an MSI package with logging and a basic UI, use the following script:

Start-Process 'msiexec.exe' -ArgumentList '/I "Path\To\Your\Package.msi" /L*V "Path\To\LogFile.log" /qb' -Wait

Silent installation of MSI packages is facilitated by the `msiexec.exe` utility, using the `/qn` switch to execute the installation with no user interface, ensuring a seamless background process.

For enhanced troubleshooting, administrators can incorporate logging through the `/l*v` option, specifying a file to capture detailed installation activities. Customization of the installation process can be achieved by setting properties directly on the command line, allowing for tailored configurations without modifying the original package.

To avoid unintended system restarts, the `/norestart` flag is essential, providing control over the post-installation environment.

Additionally, the flexibility of `msiexec` extends to repairing or uninstalling software silently, using specific switches to support maintenance tasks without user interaction.

These silent installation capabilities are integral for efficient software deployment and management in automated or controlled settings, ensuring minimal disruption while maintaining comprehensive control over the installation process.

Here, `/L*V "Path\To\LogFile.log"` instructs `msiexec` to create a verbose log file of the installation process, which can be invaluable for troubleshooting. The `/qb` argument sets the user interface level to a basic UI, offering a compromise between a silent installation and one that provides progress feedback.

NoteFor an in-depth understanding of how to use the msiexec.exe command line for managing MSI packages, check this article.

Installing MSI with PowerShell App Deployment Toolkit

The PowerShell App Deployment Toolkit (PSADT) offers a more streamlined approach to installing MSI packages. PSADT simplifies the process by wrapping installations in a PowerShell script, providing custom functions for common installation tasks. This not only makes script creation more straightforward but also enhances the user experience with additional features like deferring installations or displaying custom messages.

For example, using PSADT to install an MSI package can be as simple as calling a predefined function with the path to the MSI file as an argument. PSADT then handles the complexities of the installation process, including logging, user interaction, and error handling. This abstraction allows IT professionals to focus more on deployment strategy than script syntax's intricacies.

TipFor more details about the PowerShell App Deployment Toolkit, check out this article which goes more in depth on the topic.

Uninstalling MSI with PowerShell

PowerShell also facilitates the uninstallation of MSI packages, offering a flexible method for software removal on Windows systems.

By leveraging PowerShell's ability to interact with the Windows Installer service, administrators can automate the uninstallation process, ensuring consistency and reducing errors.

A typical method involves using the Get-WmiObject cmdlet to identify and execute the uninstall command for the desired package. This approach allows for targeted software removal based on specific criteria, such as the name or version of the software.

For example, let’s uninstall an application named "ExampleApp".

First, we use the `Get-WmiObject` cmdlet to search for the installed application. Here's how you might structure the PowerShell command:

$app = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -match "ExampleApp" }

This command searches for an installed product that matches the name "ExampleApp". If the application is found, it's assigned to the `$app` variable.

Next, to proceed with the uninstallation, we invoke the `Uninstall` method on the object contained in `$app`:

if ($app -ne $null) {
	$app.Uninstall()
} else {
	Write-Output "Application not found."
}

This method checks if the application was found (`$app -ne $null`). If the application exists, the `Uninstall()` method is called to uninstall the application. If the application is not found, it outputs a message indicating that the application was not found.

It's important to note a couple of considerations when using this method:

  • The `Win32_Product` class method can be slow and may cause a reconfiguration of installed products. It enumerates every installed product and might trigger a consistency check on each, which can impact system performance.
  • Using this approach in a script allows for automation in bulk uninstallation scenarios or as part of a larger maintenance script. However, for high-volume environments or critical systems, thorough testing is recommended to ensure compatibility and minimize disruptions.

This example demonstrates a straightforward way to uninstall software using PowerShell, leveraging the capabilities of Windows Management Instrumentation (WMI) for effective system management.

Uninstalling MSI with PowerShell App Deployment Toolkit

Using the PowerShell App Deployment Toolkit (PSADT) for uninstalling MSI packages offers enhanced control, especially in complex or large-scale environments.

PSADT provides a unified framework for managing installations, uninstallations, and application updates with extended logging, user interaction, and error-handling capabilities.

Here's how you can structure a script for uninstalling an application named "ExampleApp" using PSADT.

First, ensure you have the PSADT extracted and ready. You will be working primarily within the `Deploy-Application.ps1` script file, which is where you define your installation, uninstallation, and other custom actions.

To uninstall an MSI package with PSADT, open the `Deploy-Application.ps1` file and navigate to the `Uninstall` section.

Here's an example of what the uninstallation would look like.

[scriptblock]$UninstallApplication = {
	# Specify the MSI product code or the name of the application you wish to uninstall
	$productCode = '{12345678-90AB-CDEF-1234-567890ABCDEF}' # Example MSI product code
	$appName = "ExampleApp" # Example application name
	# Attempt to find and uninstall the application by its product code
	Execute-MSI -Action Uninstall -Path $productCode
	# Alternatively, if you don't have the product code, you can uninstall by name
	# This method relies on finding the application in the registry and then uninstalling it
	Remove-MSIApplications -Name $appName
	# Additional cleanup actions can be performed here if necessary
}

n this script, we use the `Execute-MSI` function to remove an MSI package using its unique product code, making sure we uninstall the right app.

If you don't know the product code or prefer using the app's name, the `Remove-MSIApplications` function can be used. This is handy if you're dealing with several versions of an app or if the product code isn't clear.

The PSADT offers detailed logging by default, which helps in troubleshooting and verifying the uninstallation process. Logs are typically found in the `Logs` folder within the PSADT directory, providing a comprehensive record of the script's execution.

Before running the script, ensure that the toolkit's files (`Deploy-Application.exe`, `Deploy-Application.ps1`, etc.) are located in the same directory.

To initiate the uninstallation process, run the `Deploy-Application.exe` with the `-DeploymentType Uninstall` parameter:

Deploy-Application.exe -DeploymentType Uninstall

This command triggers the execution of the `Deploy-Application.ps1` script, specifically invoking the uninstallation logic defined in the `$UninstallApplication` script block.

Conclusion

In conclusion, installing MSI packages via PowerShell offers a powerful method for software deployment, with options ranging from simple silent installations to more complex setups involving logging and user interface customization.

Additionally, the PowerShell App Deployment Toolkit further simplifies this process, making it accessible to a broader range of users.

Whether you're a seasoned IT professional or new to software deployment, these tools provide the flexibility and efficiency needed in today’s fast-paced IT environments.

Stay ahead in the world of software packaging and deployment by subscribing to our newsletter.

Click here and let the good emails roll in!

https://www.advancedinstaller.com/newsletter-subscribe.html

Written by
See author's page
Alex Marin

Application Packaging and SCCM Deployments specialist, solutions finder, Technical Writer at Advanced Installer.

Comments: