Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created April 17, 2019 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnLBevan/e6b90c5acda3799f444a8607f34d0d50 to your computer and use it in GitHub Desktop.
Save JohnLBevan/e6b90c5acda3799f444a8607f34d0d50 to your computer and use it in GitHub Desktop.
Helper for uninstalling apps / covers apps not listed in Win32_Process. Ironically created for WinRAR, but doesn't work with WinRAR, since that has its own uninstall.exe :/
#based on code from https://stackoverflow.com/questions/113542/how-can-i-uninstall-an-application-using-powershell
function Uninstall-Application {
[CmdletBinding(SupportsShouldProcess = $true)]
Param (
[Parameter(Mandatory = $true)]
[string]$AppDisplayNameFilter
)
Begin {
#assume we're running on an x64 machine, since we're in 2019
$x32Path = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$x64Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
}
Process {
[psobject[]]$foundApps = @($x32Path, $x64Path) | Join-Path -ChildPath '*' | Get-Item | Select-Object -ExpandProperty PSPath | Get-ItemProperty | Where-Object {$_.DisplayName -like $AppDisplayNameFilter}
Write-Verbose ('Found {0} apps matching Application DisplayName Filter: "{1}"' -f $foundApps.Count, $AppDisplayNameFilter)
$foundApps | ForEach-Object {
Write-Verbose ('Processing App {0}: ({1})' -f $_.DisplayName, $_.PSChildName)
[string]$uninstallCommand = $_.UninstallString
$uninstallCommand = $uninstallCommand.Trim()
$uninstallCommand = ((($uninstallCommand.Trim() -replace '^msiexec(?:\.exe)? ', '') -replace '\/I', '') -replace '\/X', '').Trim()
if ([string]::IsNullOrWhiteSpace($_.UninstallString)) {
Write-Warning ('Could not uninstall app {0}: ({1}); it has no uninstall string.' -f $_.DisplayName, $_.PSChildName)
} else {
$uninstallCommand = "/X $uninstallCommand /qb"
#Note: Until the latest version of PS WhatIf was not supported on Start-Process: https://github.com/PowerShell/PowerShell/pull/4735
if ($PSCmdlet.ShouldProcess("-FilePath 'msiexec.exe' -ArgumentList $uninstallCommand -Wait",'start-process')) {
Start-Process -FilePath 'msiexec.exe' -ArgumentList $uninstallCommand -Wait
}
}
}
}
}
Uninstall-Application -AppDisplayNameFilter 'WinRAR *' -Verbose -Confirm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment