From 46d1d2bab7aaae115e8b3c4687b05673ed5c311b Mon Sep 17 00:00:00 2001 From: Jeffrey <9938813+Raphire@users.noreply.github.com> Date: Wed, 2 Oct 2024 00:42:22 +0200 Subject: [PATCH] Improve app removal errorhandling, Additionally added a workaround for the issue described in #131, to ensure the app is atleast uninstalled for the current user while we figure out how to fix app removal for all users. --- Win11Debloat.ps1 | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/Win11Debloat.ps1 b/Win11Debloat.ps1 index 0d8107a..15b8403 100644 --- a/Win11Debloat.ps1 +++ b/Win11Debloat.ps1 @@ -367,15 +367,41 @@ function RemoveApps { # Remove installed app for all existing users if ($WinVersion -ge 22000){ # Windows 11 build 22000 or later - Get-AppxPackage -Name $app -AllUsers | Remove-AppxPackage -AllUsers -ErrorAction Continue + try { + Get-AppxPackage -Name $app -AllUsers | Remove-AppxPackage -AllUsers -ErrorAction Continue + } + catch { + Write-Host "Unable to remove $app for all users" -ForegroundColor Yellow + Write-Host $psitem.Exception.StackTrace -ForegroundColor Gray + } } else { # Windows 10 - Get-AppxPackage -Name $app -PackageTypeFilter Main, Bundle, Resource -AllUsers | Remove-AppxPackage -AllUsers + try { + Get-AppxPackage -Name $app | Remove-AppxPackage -ErrorAction SilentlyContinue + } + catch { + Write-Host "Unable to remove $app for current user" -ForegroundColor Yellow + Write-Host $psitem.Exception.StackTrace -ForegroundColor Gray + } + + try { + Get-AppxPackage -Name $app -PackageTypeFilter Main, Bundle, Resource -AllUsers | Remove-AppxPackage -AllUsers -ErrorAction SilentlyContinue + } + catch { + Write-Host "Unable to remove $app for all users" -ForegroundColor Yellow + Write-Host $psitem.Exception.StackTrace -ForegroundColor Gray + } } # Remove provisioned app from OS image, so the app won't be installed for any new users - Get-AppxProvisionedPackage -Online | Where-Object { $_.PackageName -like $app } | ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName } + try { + Get-AppxProvisionedPackage -Online | Where-Object { $_.PackageName -like $app } | ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName } + } + catch { + Write-Host "Unable to remove $app from windows image" -ForegroundColor Yellow + Write-Host $psitem.Exception.StackTrace -ForegroundColor Gray + } } }