Files
management-scripts/Uninstall-MSI.ps1
T

44 lines
1.9 KiB
PowerShell
Raw Normal View History

2021-10-29 12:00:20 -04:00
## $APP_NAME must be set by the calling script or specified here
## Get all of the uninstall registry keys
$UNINSTALL_KEYS = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
$UNINSTALL_KEYS += Get-ChildItem -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
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
$DISPLAY_NAME = $REG_PROPERTY.DisplayName
## Make sure the DisplayName matches what we're looking for
2021-10-29 12:15:22 -04:00
If ( $DISPLAY_NAME -ilike $APP_NAME ) {
2021-10-29 12:00:20 -04:00
## Make sure the software was installed using an MSI
2021-10-29 12:15:22 -04:00
If ( $REG_PROPERTY.WindowsInstaller -eq 1 ) {
2021-10-29 12:00:20 -04:00
## Get the install location
If ( $REG_PROPERTY.InstallLocation ) { $INSTALL_LOCATION = $REG_PROPERTY.InstallLocation }
## Get the product code from the UninstallString
If ( $REG_PROPERTY.UninstallString ) { $PRODUCT_CODE = $REG_PROPERTY.UninstallString.Replace('MsiExec.exe /X','') }
## Get the product code from the ModifyPath
ElseIf ( $REG_PROPERTY.ModifyPath ) { $PRODUCT_CODE = $REG_PROPERTY.ModifyPath.Replace('MsiExec.exe /X','') }
## Uninstall the app
2021-10-29 12:15:22 -04:00
If ( $PRODUCT_CODE ) {
LogMsg "Uninstalling ${DISPLAY_NAME} ${PRODUCT_CODE}"
Try { Start-Process -FilePath "msiexec.exe" -ArgumentList "/X${PRODUCT_CODE} /qn /norestart" -Wait }
Catch { LogErr $_.Exception.Message }
If ( $INSTALL_LOCATION ) {
If ( Test-Path $INSTALL_LOCATION ) {
LogMsg "Deleting install directory"
Try { Remove-Item $INSTALL_LOCATION -Recurse -Force }
Catch { LogErr $_.Exception.Message }
2021-10-29 12:00:20 -04:00
}
}
}
}
}
}