Find an uninstall string and remove an MSI silently
Search the registry for an application, show its uninstall string, and remove an MSI product silently with msiexec and a log file.
LanguagePowerShell
What it does to the systemMakes changes. Test it on one computer first.
You need the uninstall command for an application, for a package or a quick removal. The command sits in the registry under a key name you cannot guess.
This script searches both uninstall keys by name and shows what it finds. With -Uninstall it removes one MSI product silently and writes a log.
PowerShell
<#
Find the uninstall string of an application. Optional: remove an MSI silently.
Without -Uninstall the script only reads. With -Uninstall it needs administrator rights.
Example: .\Find-Uninstall.ps1 -Name '7-Zip'
Example: .\Find-Uninstall.ps1 -Name '7-Zip 23.01 (x64 edition)' -Uninstall -WhatIf
#>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param(
# Part of the display name.
[Parameter(Mandatory = $true)]
[string]$Name,
# Remove the product. Works only when exactly one MSI product matches.
[switch]$Uninstall
)
$keys = @(
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
)
$apps = @(Get-ItemProperty -Path $keys -ErrorAction SilentlyContinue |
Where-Object { $_.DisplayName -like "*$Name*" })
if (-not $Uninstall) {
$apps | Select-Object DisplayName, DisplayVersion, @{ n = 'KeyName'; e = { $_.PSChildName } }, UninstallString
return
}
# Guards: one match only, and it must be an MSI product.
if ($apps.Count -ne 1) {
Write-Warning "$($apps.Count) products match '$Name'. Use a name that matches exactly one."
return
}
$app = $apps[0]
$productCode = $app.PSChildName
if ($productCode -notmatch '^\{[0-9A-Fa-f]{8}(-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12}\}$') {
Write-Warning "'$($app.DisplayName)' is not an MSI product. Its own command is: $($app.UninstallString)"
return
}
if ($PSCmdlet.ShouldProcess($app.DisplayName, "msiexec /x $productCode /qn /norestart")) {
$log = Join-Path $env:TEMP ('uninstall-{0}.log' -f $productCode.Trim('{}'))
$arguments = '/x {0} /qn /norestart /l*v "{1}"' -f $productCode, $log
$process = Start-Process -FilePath msiexec.exe -ArgumentList $arguments -Wait -PassThru
[pscustomobject]@{ Product = $app.DisplayName; ExitCode = $process.ExitCode; Log = $log }
}
How to use it
- Save the script as
Find-Uninstall.ps1. - Run
.\Find-Uninstall.ps1 -Name 'Zip'to list the products that match. This step changes nothing. - Make the name more exact until one product is left.
- Open PowerShell as an administrator and add
-Uninstall -WhatIfto see the command. - Remove
-WhatIfto uninstall. The script asks you to confirm. Add-Confirm:$falseto skip the question in a package.
Good to know
- Exit code 0 is success. 3010 is success with a restart needed. 1605 means the product is not installed.
- For a product that is not MSI, the script shows the vendor command and stops. Silent switches differ per vendor.
- Test on one computer first. An uninstall cannot be undone.
uninstall string
msiexec
MSI
product code
silent uninstall