From 57e06ed07eb2341266704421fd7220de96741635 Mon Sep 17 00:00:00 2001 From: zoicware <118035521+zoicware@users.noreply.github.com> Date: Tue, 9 Dec 2025 23:48:13 -0500 Subject: [PATCH] additional error handling when removing cbs packages --- RemoveWindowsAi.ps1 | 46 ++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/RemoveWindowsAi.ps1 b/RemoveWindowsAi.ps1 index b5c382c..80e74ac 100644 --- a/RemoveWindowsAi.ps1 +++ b/RemoveWindowsAi.ps1 @@ -1131,23 +1131,38 @@ function Remove-AI-CBS-Packages { $regPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages' $ProgressPreference = 'SilentlyContinue' Get-ChildItem $regPath | ForEach-Object { - $value = try { Get-ItemPropertyValue "registry::$($_.Name)" -Name Visibility -ErrorAction SilentlyContinue } catch {} - if ($value -eq 2 -and $_.PSChildName -like '*AIX*' -or $_.PSChildName -like '*Recall*' -or $_.PSChildName -like '*Copilot*' -or $_.PSChildName -like '*CoreAI*') { - Set-ItemProperty "registry::$($_.Name)" -Name Visibility -Value 1 -Force - New-ItemProperty "registry::$($_.Name)" -Name DefVis -PropertyType DWord -Value 2 -Force - Remove-Item "registry::$($_.Name)\Owners" -Force -ErrorAction SilentlyContinue - Remove-Item "registry::$($_.Name)\Updates" -Force -ErrorAction SilentlyContinue - try { - Remove-WindowsPackage -Online -PackageName $_.PSChildName -NoRestart -ErrorAction Stop *>$null - Get-ChildItem "$env:windir\servicing\Packages" -Filter "*$($_.PSChildName)*" | Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue - } - catch { - #fallback to dism when user is using powershell 7 - dism.exe /Online /Remove-Package /PackageName:$($_.PSChildName) /NoRestart /Quiet - Get-ChildItem "$env:windir\servicing\Packages" -Filter "*$($_.PSChildName)*" | Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue - } + $value = try { Get-ItemPropertyValue "registry::$($_.Name)" -Name Visibility -ErrorAction Stop } catch { $null } + + if ($value -ne $null) { + if ($value -eq 2 -and $_.PSChildName -like '*AIX*' -or $_.PSChildName -like '*Recall*' -or $_.PSChildName -like '*Copilot*' -or $_.PSChildName -like '*CoreAI*') { + Set-ItemProperty "registry::$($_.Name)" -Name Visibility -Value 1 -Force + New-ItemProperty "registry::$($_.Name)" -Name DefVis -PropertyType DWord -Value 2 -Force + Remove-Item "registry::$($_.Name)\Owners" -Force -ErrorAction SilentlyContinue + Remove-Item "registry::$($_.Name)\Updates" -Force -ErrorAction SilentlyContinue + try { + Remove-WindowsPackage -Online -PackageName $_.PSChildName -NoRestart -ErrorAction Stop *>$null + $paths = Get-ChildItem "$env:windir\servicing\Packages" -Filter "*$($_.PSChildName)*" -ErrorAction SilentlyContinue + foreach ($path in $paths) { + if ($path) { + Remove-Item $path.FullName -Force -ErrorAction SilentlyContinue + } + } + + } + catch { + #fallback to dism when user is using powershell 7 + dism.exe /Online /Remove-Package /PackageName:$($_.PSChildName) /NoRestart /Quiet + $paths = Get-ChildItem "$env:windir\servicing\Packages" -Filter "*$($_.PSChildName)*" -ErrorAction SilentlyContinue + foreach ($path in $paths) { + if ($path) { + Remove-Item $path.FullName -Force -ErrorAction SilentlyContinue + } + } + } + } } + } } } @@ -2229,6 +2244,7 @@ else { 'WorkloadsSessionHost.exe' 'WebViewHost.exe' 'aimgr.exe' + 'AppActions.exe' ) foreach ($procName in $aiProcesses) { taskkill /im $procName /f *>$null