# uninstallProgramUsingId.ps1

$appName='Virtual Machine Manager Agent'
$uninstallStringRegPaths='HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstallStrings=Get-ChildItem -Path $uninstallStringRegPaths
$uninstallString=($uninstallStrings|Get-ItemProperty|Where-Object {$_.DisplayName -match $appName}).UninstallString
if($uninstallString.count -eq 1){
    $appCode=[regex]::match($uninstallString,'\{(.*)\}').Value
    $uninstallCommand="& msiexec.exe /x $appCode /quiet /norestart"
    write-host "Invoking uninstall Command: $uninstallCommand"
    Invoke-Expression $uninstallCommand
    $appStillExists=Get-WmiObject -Class Win32_Product -Filter "Name='$appName'"
    if($appStillExists){
        write-warning "Uninstall has been unsuccessful at removing $appName"
        return $false
    }else{
        return $true
    }
}else{
    write-warning "Please check this/these uninstall string(s):`r`n$uninstallString"
    return $false
}