How to Install and Uninstall MSI Packages using Powershell

install msi files with powershell

Written by Alex Marin · February 29, 2024

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

In this article, I’ll walk you through the process of installing MSI packages using PowerShell, covering basic installations and those that require arguments. We will also explore PowerShell Application Deployment Toolkit (PSADT), which further simplifies the installation process.

Installing MSI packages via PowerShell

Installing an MSI package using PowerShell is simple with the ‘Start-Process’ cmdlet. This cmdlet starts the process of installing an MSI file. For a basic installation with no 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 tool for installing, modifying, and managing Windows Installer packages.

of `/I’ switch indicates that you want to install the specified package. The path to the MSI file should be provided instead of “Path\To\Your\Package.msi”. ‘/ qnThe ` argument sets the UI level to no UI, making the installation process run silently.

For installations that require specific arguments, such as login 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 facilitated by the `msiexec.exe` tool, using the `/qn` switch to run the installation without a user interface, ensuring an uninterrupted background process.

For improved troubleshooting, administrators can enable logging via `/l*vThe ` 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 customized configurations without modifying the original package.

To avoid unwanted system reboots, `/norestart` The flag is essential, providing control over the environment after installation.

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

These silent installation capabilities are integral to 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“`guides'”.msiexec` to create a summary log file of the installation process, which can be invaluable for troubleshooting. of `/qb` The argument sets the user interface level to a basic interface, providing 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 to manage MSI packages, check out this article.

Installing MSI with PowerShell App Deployment Toolkit

The PowerShell Application Deployment Toolkit (PSADT) provides a more efficient 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 improves the user experience with additional features such as deferring installations or displaying custom messages.

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

counselFor more details about the PowerShell Application 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, providing a flexible method for removing software on Windows systems.

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

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

For example, let’s uninstall an app called “SampleApp”.

First, we use the `Get-WmiObject` cmdlet to search for the installed application. Here’s how you can 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 “Sample App“. If the application is found, it is assigned to “$app` variable.

Then, to proceed with the uninstallation, we call `uninstallMethod ` on object contained in `$app`:

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

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

It is important to take into account several considerations when using this method:

  • The `Win32_Product` class method may be slow and may cause a reconfiguration of installed products. It counts each product installed and can trigger a consistency check for each, which can affect system performance.
  • Using this approach in a script allows for automation in bulk uninstall 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 downtime.

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

Uninstalling the MSI with the PowerShell Application Deployment Toolkit

Using the PowerShell Application Deployment Toolkit (PSADT) to uninstall MSI packages provides improved control, especially in complex or large-scale environments.

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

Here’s how you can structure a script to uninstall an application named “ExampleApp” using PSADT.

First, make sure you have PSADT extracted and ready. You will primarily work within the `Deploy-Application.ps1` The script file, which is where you define installation, uninstallation, and other custom actions.

To uninstall an MSI package with PSADT, run `Deploy-Application.ps1` file and navigate to `uninstallsection `.

Here’s an example of what the uninstall 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
}

In this script, we use the “Execute-MSI” function to remove an MSI package using the unique product code, making sure to uninstall the correct application.

If you do not know the product code or prefer to use the application name, the “Remove-MSIApplications” function can be used. This is useful if you are dealing with several versions of an application or if the product code is not clear.

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

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

To start the uninstall process, run “Deploy-Application.exe” with the “-DeploymentType Uninstall” parameter:

Deploy-Application.exe -DeploymentType Uninstall

This command causes `Deploy-Application.ps1` script, specifically calling the uninstall logic defined in `$UninstallApplication` script block.

CONCLUSION

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

Additionally, the PowerShell Application Deployment Toolkit further simplifies this process, making it accessible to a wider 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 of the curve 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
Check out the author's page
Alex Marin

Application Packaging and SCCM Deployment Specialist, Solution Finder, Technical Writer at Advanced Installer.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Contact Me on Zalo
Tư Vấn Miễn Phí