function Invoke-WPFUnInstall { param( [Parameter(Mandatory=$false)] [PSObject[]]$PackagesToUninstall = $($sync.selectedApps | Foreach-Object { $sync.configs.applicationsHashtable.$_ }) ) <# .SYNOPSIS Uninstalls the selected programs #> if($sync.ProcessRunning) { $msg = "[Invoke-WPFUnInstall] Install process is currently running" Show-WinUtilMessage -Message $msg -Title "Winutil" -Button "OK" -Icon "Warning" return } if ($PackagesToUninstall.Count -eq 0) { $WarningMsg = "Please select the program(s) to uninstall" Show-WinUtilMessage -Message $WarningMsg -Title $AppTitle -Button "OK" -Icon "Warning" return } $ButtonType = "YesNo" $MessageboxTitle = "Are you sure?" $Messageboxbody = ("This will uninstall the following applications: `n $($PackagesToUninstall | Select-Object Name, Description| Out-String)") $MessageIcon = "Information" $confirm = Show-WinUtilMessage -Message $Messageboxbody -Title $MessageboxTitle -Button $ButtonType -Icon $MessageIcon if($confirm -eq "No") {return} $ManagerPreference = $sync.preferences.packagemanager Write-WinUtilLog -Component "Uninstall" -Message "Uninstall requested for $(@($PackagesToUninstall).Count) selected package(s) using preference: $ManagerPreference" $packageSummary = Get-WinUtilPackageLogSummary -Packages $PackagesToUninstall -Preference $ManagerPreference Write-WinUtilLog -Component "Uninstall" -Message "Uninstall selected package(s): $($packageSummary -join '; ')" Invoke-WPFRunspace -ParameterList @(("PackagesToUninstall", $PackagesToUninstall),("ManagerPreference", $ManagerPreference)) -ScriptBlock { param($PackagesToUninstall, $ManagerPreference) $packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToUninstall -Preference $ManagerPreference $packagesWinget = $packagesSorted['Winget'] $packagesChoco = $packagesSorted['Choco'] $totalPackages = @($packagesWinget).Count + @($packagesChoco).Count $completedPackages = 0 $hasUI = $null -ne $sync.Form -and $null -ne $sync.Form.Dispatcher Write-WinUtilLog -Component "Uninstall" -Message "Uninstall package manager split: winget=$(@($packagesWinget).Count), choco=$(@($packagesChoco).Count)" try { $sync.ProcessRunning = $true if ($hasUI) { Set-WinUtilTweaksProgressIndicator -Visible $true -Label "Preparing app uninstall (0/$totalPackages)" -Percent 0 Invoke-WPFUIThread -ScriptBlock { if ($null -ne $sync.ItemsControl) { $sync.ItemsControl.IsEnabled = $false } } } if ($packagesWinget -contains "Microsoft.Edge") { New-Item -Path "$Env:SystemRoot\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe" -Force } # Uninstall all selected programs in new window if($packagesWinget.Count -gt 0) { foreach ($program in $packagesWinget) { $position = $completedPackages + 1 $startPercent = [int](($completedPackages / $totalPackages) * 100) if ($hasUI) { Set-WinUtilTweaksProgressIndicator -Visible $true -Label "Uninstalling $program ($position/$totalPackages)" -Percent $startPercent } Install-WinUtilProgramWinget -Action Uninstall -Programs @($program) $completedPackages++ $completedPercent = [int](($completedPackages / $totalPackages) * 100) if ($hasUI) { Set-WinUtilTweaksProgressIndicator -Visible $true -Label "Uninstalled $program ($completedPackages/$totalPackages)" -Percent $completedPercent Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -value ($completedPercent / 100) } } } } if($packagesChoco.Count -gt 0) { $position = $completedPackages + 1 $startPercent = [int](($completedPackages / $totalPackages) * 100) if ($hasUI) { Set-WinUtilTweaksProgressIndicator -Visible $true -Label "Uninstalling Chocolatey packages ($position/$totalPackages)" -Percent $startPercent } Install-WinUtilProgramChoco -Action Uninstall -Programs $packagesChoco $completedPackages += @($packagesChoco).Count $completedPercent = [int](($completedPackages / $totalPackages) * 100) if ($hasUI) { Set-WinUtilTweaksProgressIndicator -Visible $true -Label "Uninstalled Chocolatey packages ($completedPackages/$totalPackages)" -Percent $completedPercent Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -value ($completedPercent / 100) } } } Write-Host "===========================================" Write-Host "-- Uninstalls have finished ---" Write-Host "===========================================" Write-WinUtilLog -Component "Uninstall" -Message "Uninstall workflow completed." if ($hasUI) { Set-WinUtilTweaksProgressIndicator -Visible $true -Label "App uninstall finished" -Percent 100 Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" } } } catch { Write-Host "===========================================" Write-Host "Error: $_" Write-Host "===========================================" Write-WinUtilLog -Level "ERROR" -Component "Uninstall" -Message "Uninstall workflow failed: $($_.Exception.Message)" if ($hasUI) { Set-WinUtilTweaksProgressIndicator -Visible $true -Label "App uninstall failed" -Percent 100 Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" -overlay "warning" } } } finally { if ($hasUI) { Invoke-WPFUIThread -ScriptBlock { if ($null -ne $sync.ItemsControl) { $sync.ItemsControl.IsEnabled = $true } } } $sync.ProcessRunning = $False } } }