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
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.
Start-Process 'msiexec.exe' -ArgumentList '/I "Path\To\Your\Package.msi" /L*V "Path\To\LogFile.log" /qb' -Wait
To avoid unintended system restarts, the `/norestart` flag is essential, providing control over the post-installation environment.
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.
For 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
Uninstalling MSI with PowerShell
PowerShell also facilitates the uninstallation of MSI packages, offering a flexible method for software removal on Windows systems.
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.
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
Conclusion
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.
Popular Articles
Here’s the Script to check for old modules, remove them and install new ones:
# Get a list of all installed modules
$installedModules = Get-InstalledModule
$totalModules = $installedModules.Count
$currentModule = 0
foreach ($module in $installedModules) {
$currentModule++
# Get all versions of the module
$allVersions = Get-InstalledModule -Name $module.Name -AllVersions
# If there's more than one version installed
if ($allVersions.Count -gt 1) {
# Sort by version and select all but the latest version
$oldVersions = $allVersions | Sort-Object {[Version]$_} | Select-Object -First ($allVersions.Count - 1)
# Uninstall old versions
foreach ($oldVersion in $oldVersions) {
Write-Host "Uninstalling $($oldVersion.Name) version $($oldVersion.Version)..."
Uninstall-Module -Name $oldVersion.Name -RequiredVersion $oldVersion.Version -Force
}
}
# Install the latest version
Write-Host "Installing latest version of $($module.Name)..."
Install-Module -Name $module.Name -Force -AllowClobber -SkipPublisherCheck
# Update progress bar
Write-Progress -PercentComplete (($currentModule / $totalModules) * 100) -Status "Processing Modules" -Activity "Processed $currentModule of $totalModules"
}
Write-Progress -Completed
Write-Host "Script completed!"
What is a PowerShell module?
Introduction to PowerShell modules: A PowerShell module is a collection of cmdlets, functions, and scripts that are grouped together for easy management and distribution. Modules are designed to enhance the capabilities of PowerShell, allowing you to perform specific tasks more efficiently.
Why should you remove old PowerShell modules? Removing old PowerShell modules is essential for maintaining a clean and streamlined environment. It helps prevent conflicts between different versions of the same module, reduces clutter in your module list, and ensures you are using the latest and most stable versions of modules.
How to uninstall a PowerShell module?
Using the Uninstall-Module command: The Uninstall-Module command is the primary way to remove a PowerShell module. It allows you to specify the module to remove and provides options to control the removal process.
Specifying the module name to remove: To uninstall a specific module, you need to provide its name using the -Name parameter. For example, to uninstall a module named “ExampleModule”, you would run the command “Uninstall-Module -Name ExampleModule”.
Removing all versions of a module: If you want to remove all versions of a module, you can use the -AllVersions parameter. This ensures that every version of the specified module is uninstalled.
Using the -Force parameter
Understanding the -Force parameter: The -Force parameter is used to forcibly uninstall a module, even if it is in use or required by other modules or scripts. It overrides any warnings or restrictions and uninstalls the module.
The impact of using the -Force parameter: While the -Force parameter can be handy in certain situations, it should be used with caution. Removing a module forcefully may break dependencies and cause unexpected behavior in PowerShell sessions or scripts.
Scenarios where using the -Force parameter is recommended: The -Force parameter is commonly used when you are sure about the consequences of removing a module forcefully or when troubleshooting module-related issues that cannot be resolved otherwise.
How to retrieve a list of installed PowerShell modules?
Using the Get-InstalledModule command: The Get-InstalledModule command allows you to retrieve a list of installed PowerShell modules on your system. It provides information such as the module name, version, and installed location.
Filtering the module list based on the version: If you want to filter the module list based on a specific version, you can use the -RequiredVersion parameter. This helps you identify modules with older versions that can be removed.
Exporting the module list to a file: To save the module list for reference or analysis, you can export it to a file using the > (output to file) operator. For example, “Get-InstalledModule > modulelist.txt” exports the list to a file named “modulelist.txt”.
FAQ – Using PowerShell Uninstall
Q: What is PowerShellGet?
A: PowerShellGet is a module in Windows PowerShell that enables you to manage modules, DSC resources, and scripts in a simple, unified way.
Q: How do I remove old PowerShell modules?
A: You can remove old PowerShell modules using the Remove-Module cmdlet.
Q: How can I view the available versions of a PowerShell module?
A: You can use the Get-Module command with the -ListAvailable parameter to view the available versions of a PowerShell module.
Q: Can I remove multiple versions of a module using PowerShell?
Q: How do I remove a PowerShell module from the local computer?
Q: Can I remove a module using a wildcard pattern?
A: Yes, you can remove a module using a wildcard pattern by specifying the pattern in the -Name parameter of the Remove-Module cmdlet.
Q: How do I remove a specific version of a module using PowerShell?
A: You can specify the module version in the -RequiredVersion parameter of the Remove-Module cmdlet to remove a specific version of a module.
Q: What is the -Verbose parameter used for when removing a module?
A: The -Verbose parameter is used to display detailed information about the removal process.
Q: Can I remove a module without being prompted for confirmation?
A: Yes, you can use the -Confirm parameter with a value of $false to remove a module without being prompted for confirmation.
Q: Can I use the -WhatIf parameter to see the changes that will be made when removing a module?
A: Yes, you can use the -WhatIf parameter with the Remove-Module cmdlet to see the changes that will be made without actually removing the module.
keywords: Delete and uninstall the module string using microsoft module installed powershellget module in github pipeline prerelease requiredversion verbose ps answer you’re looking
Uninstall Microsoft Graph PowerShell Modules Completely
In some cases, you no longer work with Microsoft Graph PowerShell and want to remove them from your computer completely. Or you want to uninstall the older versions of the module to install the latest version.
First, we get the list of the installed modules with all versions. As you can see below, over time, when update the module, the older versions are not got removed automatically. So, we’ve six versions of the module.
Name Version ModuleType
---- ------- ----------
Microsoft.Graph.Authentication 2.5.0 Script
Microsoft.Graph.Authentication 1.28.0 Script
Microsoft.Graph.Authentication 1.27.0 Script
Microsoft.Graph.Authentication 1.25.0 Script
Microsoft.Graph.Authentication 2.4.0 Script
Microsoft.Graph.Authentication 2.3.0 Script
Remove the Microsoft Graph PowerShell module is a little bit complicated. But don’t worry, we’ve created a PowerShell script to do it automatically. The script will:
- Uninstall all Microsoft Graph modules except Microsoft.Graph.Authentication (because this module is the dependency module of others one. So, it can be uninstalled when all other modules are got removed only).
- When uninstalling the modules, some modules cannot be uninstalled because the dependencies. So, we need to remove them with Get-InstalledModule results.
- Finally, once all other modules got removed, we can uninstall the Microsoft,Graph.Authentication module.
Below is a PowerShell script do remove all installed Microsoft PowerShell SDK version on your computer.
1️⃣ To do it, copy the below script => Open Notepad (or any your favorite text editor) => Paste the script then save it as a .ps1 file. The file format of a PowerShell script is *.ps1.
#Uninstall Microsoft.Graph.Authentication
$ModuleName = "Microsoft.Graph.Authentication"
$Versions = Get-Module $ModuleName -ListAvailable
Foreach ($Version in $Versions)
$ModuleVersion = $Version.Version
Write-Host "Uninstall-Module $ModuleName $ModuleVersion"
Uninstall-Module $ModuleName -RequiredVersion $ModuleVersion
Note
Note: You must close all opening PowerShell window, then open a new one then run the uninstall script. Otherwise, you would get errors.
×
Dismiss this alert.
3️⃣ Run the saved script. For example, the path of our script is E:scriptsgraph_cleanup.ps1.
4️⃣ (Optional) In some cases, when run the script, you would get the error the script cannot be loaded because running script is disabled on this sysstem.
To fix it, we need to enable running script on your system using the below command. You can get more about the Execution Policy from this post.
Set-ExecutionPolicy RemoteSigned -Force
If you get the below errors while uninstalling the Microsoft Grap module. Let’s close all opening PowerShell window (or restart your computer) the re-run the script.
WARNING: The version ‘2.4.0’ of module ‘Microsoft.Graph.Beta.Identity.DirectoryManagement’ is currently in use. Retry the operation after closing the applications.
PackageManagementUninstall-Package : Module ‘Microsoft.Graph.Beta.Identity.SignIns’ is in currently in use or you don’t have the required permissions.
5️⃣ The script will uninstall the modules automatically.
Note
Note: in our case, it takes one hour to complete, so please be patient.
×
Dismiss this alert.
6️⃣ Once done, you can run the below command again to check all modules have been uninstalled.
Reinstall the Microsoft PowerShell module
To reinstall the latest version of Microsoft Graph module, run the below command:
Install-Module Microsoft.Graph -force
Install-Module Microsoft.Graph.Beta -Force
Uninstall script
If you don’t want to create your own PowerShell script manually. We’ve created a PowerShell script for you. All you need to do is open PowerShell then execute the below command.
Not a reader? Watch this related video tutorial: