Catalin
Posts: 7030
Joined: Wed Jun 13, 2018 7:49 am

How to use PowerShell for Package Management - Beginner oriented

Hello,

For many users, installing and uninstalling software involves navigating menus and clicking buttons. But beneath the graphical interface (GUI) lies a powerful command-line world, and within that world resides PowerShell. PowerShell offers an efficient and customizable way to manage software on your Windows system, giving you finer control over installations and uninstalls.

Before diving into PowerShell's magic, let's take a brief detour through the classic Windows command prompt. While not as user-friendly and powerful as PowerShell, it makes use of the msiexec.exe tool for basic MSI package installations. However, it lacks the flexibility and automation that PowerShell provides.

This article will guide you through leveraging PowerShell's capabilities for installing and uninstalling of MSI packages. We'll also explore logging options for MSI installations to keep track of the process.

PowerShell offers a powerful way to manage software on your Windows system. This article will guide you through installing and uninstalling both MSI and EXE packages using PowerShell, giving you greater control over your system's applications.

1. MSI Packages Installation

MSI (Microsoft Installer) packages are a common format for installing software on Windows. PowerShell utilizes the msiexec.exe tool for MSI installations.

Here's how we can install an MSI package using PowerShell. As previously mentioned, we will make use of msiexec nad therefore we'll be using Start-Process cmdlet from PowerShell.

Code: Select all

Start-Process msiexec.exe -Wait -ArgumentList "/i <path_to_msi_file> <installation_arguments>"
where, for <installation_arguments> we have two more noticeable options:
  • /qn
that will install the MSI silently (with no UI).

Note: In order to install an MSI silently, the PowerShell prompt should be launched as an administrator.
  • /L*V <path_to_log_file>
that will create a verbose log for the installation process. This is mostly use to debug installation errors.

For more of the msiexec arguments, please refer to the following article: MsiExec.exe Command Line

2. MSI Packages Uninstallation

Uninstalling an MSI package can be done in more ways. The first way we will discover today is the use of /x argument of msiexec, which as opposed to /i means uninstall.

Approach #1

Code: Select all

Start-Process msiexec.exe -Wait -ArgumentList "/x <path_to_msi_file> /qn"
The biggest downside of this approach is the fact that most customers will not keep the MSI package on the machine after the installation.

Approach #2

Here's when the second approach comes into play. For this approach, we will make use of Get-WmiObject cmdlet with its' Win32_Product class.

In order to identify the installed MSI, we can use multiple criterias, but today I will show you two of them: the application name and its' IdentifyingNumber (the ProductCode).

For example, if we run the following command:

Code: Select all

Get-WmiObject Win32_Product -Filter "name='MyApplication'"
then PowerShell will return the following:
Screenshot_132.png
Screenshot_132.png (18.35 KiB) Viewed 48675 times

As you can see, it gives us quite some details about the installed package.

Here's how we can run the Uninstall() method for an installed package, by using the name.

Code: Select all

$app = Get-WmiObject Win32_Product -filter "name='MyApplication'"
$app.Uninstall()
For the IdentifyingNumber, we do the same query but instead of "name", we use "IdentifyingNumber".

Approach #3

The third approach will be through using the Get-Package cmdlet with its' Uninstall-Package method.
Screenshot_133.png
Screenshot_133.png (12.89 KiB) Viewed 48675 times

Here's the command line to uninstall the MSI package:

Code: Select all

Get-Package -name "MyApplication" | Uninstall-Package

This should be it, I hope you found this article helpful.

See you in the next one.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Sample Projects”