Files
management-scripts/Uninstall-MSI.ps1
T

54 lines
2.1 KiB
PowerShell
Raw Normal View History

# $APP_NAME must be set by the calling script or specified here
2021-10-29 12:00:20 -04:00
# Get all of the uninstall registry keys
2021-10-29 12:00:20 -04:00
$UNINSTALL_KEYS = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
$UNINSTALL_KEYS += Get-ChildItem -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
2022-10-24 14:19:00 -04:00
Function Get-GUIDFromMSIUninstallString {
param([string]$str)
$start = $str.IndexOf('{') + 1
$end = $str.IndexOf('}')
Return $str.Substring($start, $end - $start)
}
2023-01-18 19:38:18 -05:00
# Track whether an app was uninstalled or not
$PRODUCTUNINSTALLED = $false
2021-10-29 12:15:22 -04:00
Foreach ( $reg in $UNINSTALL_KEYS ) {
# Get the uninstall key
2021-10-29 12:00:20 -04:00
$REG_PROPERTY = Get-ItemProperty -Path $reg.PSPath
# Get the display name of the app
2021-10-29 12:00:20 -04:00
$DISPLAY_NAME = $REG_PROPERTY.DisplayName
# Get the UninstallString
$UNINSTALLSTRING = $REG_PROPERTY.UninstallString
# Get the ModifyPath
$MODIFYPATH = $REG_PROPERTY.ModifyPath
# Make sure the DisplayName matches what we're looking for
2021-10-29 12:15:22 -04:00
If ( $DISPLAY_NAME -ilike $APP_NAME ) {
# Make sure the software was installed using an MSI
2021-10-29 12:15:22 -04:00
If ( $REG_PROPERTY.WindowsInstaller -eq 1 ) {
# Get the product code from the UninstallString
If ( $UNINSTALLSTRING ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $UNINSTALLSTRING) }
2021-10-29 12:00:20 -04:00
# Get the product code from the ModifyPath
ElseIf ( $MODIFYPATH ) { $PRODUCT_CODE = $(Get-GUIDFromMSIUninstallString -str $MODIFYPATH) }
2021-10-29 12:00:20 -04:00
# Uninstall the app
2021-10-29 12:15:22 -04:00
If ( $PRODUCT_CODE ) {
LogMsg "Uninstalling ${DISPLAY_NAME} ${PRODUCT_CODE}"
2022-10-24 14:35:12 -04:00
$arguments = '/X{' + $PRODUCT_CODE + '} /qn /norestart'
2023-01-18 19:22:52 -05:00
Try { Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -Wait ; $PRODUCTUNINSTALLED = $true }
2021-10-29 12:15:22 -04:00
Catch { LogErr $_.Exception.Message }
2021-10-29 12:00:20 -04:00
}
}
Else {
2022-10-24 14:35:12 -04:00
LogMsg "Cannot uninstall ""${DISPLAY_NAME}""`n UninstallString: ${UNINSTALLSTRING}"
}
2021-10-29 12:00:20 -04:00
}
}
If ( $PRODUCTUNINSTALLED -eq $false ) { LogMsg "No product matching ${APP_NAME} was found"}