Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Uninstalls MSI-based applications by name.
# Written by Colby Bouma
# This script attempts to search the registry for the GUID of the specified application and uninstall it with MsiExec
#
# v 010
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$ProductName,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$ProductPublisher,
[Parameter(Mandatory = $false)]
[switch]
$JustGUID = $false
)
# Determine OS architecture
$SoftwareList = @("SOFTWARE")
if ( ( Get-Ciminstance Win32_OperatingSystem ).OSArchitecture -eq "64-bit" ) {
$SoftwareList += "SOFTWARE\Wow6432Node"
}
ForEach ( $Software in $SoftwareList) {
# Grab the Uninstall entry from the Registry
$UninstallRegristryObjects = Get-ItemProperty "HKLM:\$Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object DisplayName -like "$ProductName"
$ProductInfo = @{}
# Set these to a default value in case the Uninstall entry is invalid or missing
$ProductInfo.DisplayName = "Unknown"
$ProductInfo.GUID = "Unknown"
$ProductInfo.Install_Directory = "Unknown"
$ProductInfo.Version = "Unknown"
# Check the Uninstall entry
if ( $UninstallRegristryObjects -ne $null ) {
ForEach ( $UninstallRegristryObject in $UninstallRegristryObjects ) {
# Make sure the Publisher matches
if ( $UninstallRegristryObject.Publisher -like "$ProductPublisher" ) {
$ProductInfo.DisplayName = $UninstallRegristryObject.DisplayName
$ProductInfo.GUID = $UninstallRegristryObject.PSChildName
$ProductInfo.Install_Directory = $UninstallRegristryObject.InstallLocation
$ProductInfo.Version = $UninstallRegristryObject.DisplayVersion
} else {
Write-Output "ERROR: The Publisher does not match!"
Write-Output $UninstallRegristryObject
Exit 10
}
if ( $JustGUID ) {
Write-Output ( $ProductInfo.GUID -replace "{", "" -replace "}", "" )
} else {
Write-Output "INFO: GUID --- $($ProductInfo.GUID)"
Write-Output "INFO: Uninstalling --- $($ProductInfo.DisplayName) $($ProductInfo.Version)"
# Uninstall
Start-Process -Wait -FilePath "MsiExec.exe" -ArgumentList "/X $($ProductInfo.GUID) /qn /norestart"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.