Skip to content

Commit

Permalink
feat(msiexec): check multiple keys
Browse files Browse the repository at this point in the history
  • Loading branch information
he3als committed Nov 28, 2023
1 parent 7be0368 commit 726eca4
Showing 1 changed file with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,35 @@
#>

function Uninstall-MsiexecAppByName {
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Name
)

if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) {
throw "Uninstall-MsiexecAppByName: Not running as an administrator!"
}

$uninstallKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
$uninstallKeys = Get-ChildItem -Path $uninstallKeyPath -EA SilentlyContinue

foreach ($key in $uninstallKeys.PSPath) {
$displayName = (Get-ItemProperty -Path "$key").DisplayName
if ($displayName -like "*$Name*") {
$uninstallString = (Get-ItemProperty -Path "$key").UninstallString
if ($uninstallString -like "*MsiExec.exe*") {
$foundKey = $key | Split-Path -Leaf
Write-Warning "Uninstalling $displayName..."
Start-Process -FilePath "msiexec.exe" -ArgumentList "/qn /X$foundKey REBOOT=ReallySuppress /norestart" 2>&1 | Out-Null
}
}
}

if ($null -eq $foundKey) {
throw "Uninstall-MsiexecAppByName: No app found with an MSIExec uninstaller with the display name containing '$Name'."
}
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Name
)

if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) {
throw "Uninstall-MsiexecAppByName: Not running as an administrator!"
}

$uninstallKeyPath = "Microsoft\Windows\CurrentVersion\Uninstall"
$uninstallKeys = (Get-ChildItem -Path @(
"HKLM:\SOFTWARE\$uninstallKeyPath",
"HKLM:\SOFTWARE\WOW6432Node\$uninstallKeyPath",
"HKCU:\SOFTWARE\$uninstallKeyPath",
"HKCU:\SOFTWARE\WOW6432Node\$uninstallKeyPath"
) -EA SilentlyContinue) -match "\{\b[A-Fa-f0-9]{8}(?:-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}\b\}"

foreach ($key in $uninstallKeys.PSPath) {
$displayName = (Get-ItemProperty -Path $key).DisplayName
if (($displayName -like "*$Name*") -and ((Get-ItemProperty -Path $key).UninstallString -like "*MsiExec.exe*")) {
Write-Output "Uninstalling $displayName..."
Start-Process -FilePath "msiexec.exe" -ArgumentList "/qn /X$(Split-Path -Path $key -Leaf) REBOOT=ReallySuppress /norestart" 2>&1 | Out-Null
}
}

if ($null -eq $foundKey) {
throw "Uninstall-MsiexecAppByName: No app found with an MSIExec uninstaller with the display name containing '$Name'."
}
}

Export-ModuleMember -Function Uninstall-MsiexecAppByName

0 comments on commit 726eca4

Please sign in to comment.