Classes/HpSoftwareManager.ps1

class HpSoftwareManager {
    [LibraryContext]$Context
    
    HpSoftwareManager([LibraryContext]$context) {
        $this.Context = $context
    }

    [void]Uninstall() {
        # https://gist.github.com/mark05e/a79221b4245962a477a49eb281d97388
        $apps = @(
            # HP Bloatware
            "HP Audio Control"
            "HP Client Security Manager"
            "HP Connection Optimizer"
            "HP Dock Accessory WMI Provider"
            "HP Documentation"
            "HP Insights Analytics - Dependencies"
            "HP Insights Analytics"
            "HP Insights"
            "HP JumpStarts"
            "HP MAC Address Manager"
            "HP Notifications"
            "HP Security Update Service"
            "HP Sure Click"
            "HP Sure Recover"
            "HP Sure Run Module"
            "HP Sure Sense Installer"
            "HP Wolf Security"
            "HP Wolf Security - Console"

            "AD2F1837.HPEasyClean"
            "AD2F1837.HPJumpStarts"
            "AD2F1837.HPPCHardwareDiagnosticsWindows"
            "AD2F1837.HPPowerManager"
            "AD2F1837.HPPrinterControl"
            "AD2F1837.HPPrivacySettings"
            "AD2F1837.HPProgrammableKey"
            "AD2F1837.HPProtectTools"
            "AD2F1837.HPQuickDrop"
            "AD2F1837.HPSupportAssistant"
            "AD2F1837.HPSystemInformation"
            "AD2F1837.HP_Documentation"
            "AD2F1837.myHP"
        )
        # used for uninstall of HP Connection Optimizer
        # check out this for more info https://gist.github.com/mark05e/a79221b4245962a477a49eb281d97388
        $ConnOpt = "[InstallShield Silent]
        Version=v7.00
        File=Response File
        [File Transfer]
        OverwrittenReadOnly=NoToAll
        [{6468C4A5-E47E-405F-B675-A70A70983EA6}-DlgOrder]
        Dlg0={6468C4A5-E47E-405F-B675-A70A70983EA6}-SdWelcomeMaint-0
        Count=3
        Dlg1={6468C4A5-E47E-405F-B675-A70A70983EA6}-MessageBox-0
        Dlg2={6468C4A5-E47E-405F-B675-A70A70983EA6}-SdFinishReboot-0
        [{6468C4A5-E47E-405F-B675-A70A70983EA6}-SdWelcomeMaint-0]
        Result=303
        [{6468C4A5-E47E-405F-B675-A70A70983EA6}-MessageBox-0]
        Result=6
        [Application]
        Name=HP Connection Optimizer
        Version=2.0.18.0
        Company=HP Inc.
        Lang=0409
        [{6468C4A5-E47E-405F-B675-A70A70983EA6}-SdFinishReboot-0]
        Result=1
        BootOption=0"


        $appxprovisionedpackages = Get-AppxProvisionedPackage -Online

        foreach ($app in $apps) {
            $this.Context.LogManager.Log("Trying to remove $app")

            # remove appx packages
            if ((Get-AppxPackage -AllUsers).Name -eq "$app") {
                Get-AppxPackage $app -AllUsers | Remove-AppxPackage -AllUsers | Out-Null
                if ($?) {
                    $this.Context.LogManager.Log("Successfully removed AppX-Package $app")
                    Continue
                } else {
                    $this.Context.LogManager.Log("Failed to remove AppX-Package $app", "ERROR")
                    Continue
                }

                ($appxprovisionedpackages).Where( {$_.DisplayName -EQ $app}) |
                    Remove-AppxProvisionedPackage -Online
                if (!$?) {
                    $this.Context.LogManager.Log("Failed to remove AppX-ProvisionedPackage $app", "ERROR")
                } else {
                    $this.Context.LogManager.Log("Removed AppX-ProvisionedPackage $app")
                }
                Continue
            }

            # remove normal packages
            Get-Package -Name $app -ErrorAction SilentlyContinue | Out-Null
            if ($?) {
                Uninstall-Package -Name $app -AllVersions -Force
                if (!$?) {
                    $this.Context.LogManager.Log("Failed to remove $app", "ERROR")
                } else {
                    # in some cases uninstall command returns a successfull but doesnt actually uninstall the Package
                    # https://github.com/OneGet/oneget/issues/435
                    # as a last effort i try to remove the Package using the UninstallString
                    Get-Package -Name $app -ErrorAction SilentlyContinue | Out-Null
                    if ($?) {
                        $this.Context.LogManager.Log("Could not remove Package due to bug. Trying via Uninstallstring...", "WARN")
                        # remove Programms via uninstallString because Remove-Package doesnt work :(
                        if ($app -eq "HP Documentation") {
                            try {
                                $uninstallString = "C:\Program Files\HP\Documentation\Doc_uninstall.cmd"
                                Start-Process -FilePath "$uninstallString" -NoNewWindow
                                $this.Context.LogManager.Log("Successfully removed HP Documentation via uninstall string")
                            } catch {
                                $this.Context.LogManager.Log("Failed to remove HP Documentation via uninstall string", "ERROR")
                            }
                        } elseif ($app -eq "HP Connection Optimizer") {
                            try {
                                $ConnOpt | Out-File c:\Windows\Temp\ISS-HP.iss
                                &'C:\Program Files (x86)\InstallShield Installation Information\{6468C4A5-E47E-405F-B675-A70A70983EA6}\setup.exe' @('-s', '-f1C:\Windows\Temp\ISS-HP.iss')
                                
                                $this.Context.LogManager.Log("Successfully removed HP Connection Optimizer via uninstallfile")
                            } catch {
                                $this.Context.LogManager.Log("Could not create uninstallfile for HP Connection Optimizer", "ERROR")
                            }
                        }
                    }   
                }
            }
        }
    }
}