Speed Up Appx Removal

This commit is contained in:
zoicware
2025-05-16 15:50:45 -04:00
committed by GitHub
parent 45c2b69d57
commit 3878c78964

View File

@@ -105,21 +105,7 @@ foreach ($key in $keys) {
$aipackages = @(
'MicrosoftWindows.Client.Photon'
'MicrosoftWindows.Client.AIX'
'MicrosoftWindows.Client.CoPilot'
'Microsoft.Windows.Ai.Copilot.Provider'
'Microsoft.Copilot'
'Microsoft.MicrosoftOfficeHub'
'MicrosoftWindows.Client.CoreAI'
)
$provisioned = get-appxprovisionedpackage -online
$appxpackage = get-appxpackage -allusers
$eol = @()
$store = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore'
$users = @('S-1-5-18'); if (test-path $store) { $users += $((Get-ChildItem $store -ea 0 | Where-Object { $_ -like '*S-1-5-21*' }).PSChildName) }
#disable copilot policies in region policy json #disable copilot policies in region policy json
@@ -150,55 +136,106 @@ if (Test-Path $JSONPath) {
} }
#to make this part faster make a txt file in temp with chunck of removal
#code and then just run that from run
#trusted function due to the design of having it hidden from the user
$packageRemovalPath = "$env:TEMP\aiPackageRemoval.ps1"
if (!(test-path $packageRemovalPath)) {
New-Item $packageRemovalPath -Force | Out-Null
}
#needed for separate powershell sessions
$aipackages = @(
'MicrosoftWindows.Client.Photon'
'MicrosoftWindows.Client.AIX'
'MicrosoftWindows.Client.CoPilot'
'Microsoft.Windows.Ai.Copilot.Provider'
'Microsoft.Copilot'
'Microsoft.MicrosoftOfficeHub'
'MicrosoftWindows.Client.CoreAI'
)
$code = @'
$aipackages = @(
'MicrosoftWindows.Client.Photon'
'MicrosoftWindows.Client.AIX'
'MicrosoftWindows.Client.CoPilot'
'Microsoft.Windows.Ai.Copilot.Provider'
'Microsoft.Copilot'
'Microsoft.MicrosoftOfficeHub'
'MicrosoftWindows.Client.CoreAI'
)
$provisioned = get-appxprovisionedpackage -online
$appxpackage = get-appxpackage -allusers
$eol = @()
$store = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore'
$users = @('S-1-5-18'); if (test-path $store) { $users += $((Get-ChildItem $store -ea 0 | Where-Object { $_ -like '*S-1-5-21*' }).PSChildName) }
#use eol trick to uninstall some locked packages #use eol trick to uninstall some locked packages
foreach ($choice in $aipackages) { foreach ($choice in $aipackages) {
Write-Host "Removing $choice"
foreach ($appx in $($provisioned | Where-Object { $_.PackageName -like "*$choice*" })) { foreach ($appx in $($provisioned | Where-Object { $_.PackageName -like "*$choice*" })) {
$PackageName = $appx.PackageName $PackageName = $appx.PackageName
$PackageFamilyName = ($appxpackage | Where-Object { $_.Name -eq $appx.DisplayName }).PackageFamilyName $PackageFamilyName = ($appxpackage | Where-Object { $_.Name -eq $appx.DisplayName }).PackageFamilyName
Run-Trusted -command "New-Item `"$store\Deprovisioned\$PackageFamilyName`" -force" New-Item "$store\Deprovisioned\$PackageFamilyName" -force
Start-Sleep .5
Run-Trusted -command "dism /online /set-nonremovableapppolicy /packagefamily:$PackageFamilyName /nonremovable:0" dism /online /set-nonremovableapppolicy /packagefamily:$PackageFamilyName /nonremovable:0
Start-Sleep .5
foreach ($sid in $users) { foreach ($sid in $users) {
Run-Trusted -command "New-Item `"$store\EndOfLife\$sid\$PackageName`" -force" New-Item "$store\EndOfLife\$sid\$PackageName" -force
Start-Sleep .5
} }
$eol += $PackageName $eol += $PackageName
Run-Trusted -command "remove-appxprovisionedpackage -packagename $PackageName -online -allusers" remove-appxprovisionedpackage -packagename $PackageName -online -allusers
} }
foreach ($appx in $($appxpackage | Where-Object { $_.PackageFullName -like "*$choice*" })) { foreach ($appx in $($appxpackage | Where-Object { $_.PackageFullName -like "*$choice*" })) {
$PackageFullName = $appx.PackageFullName $PackageFullName = $appx.PackageFullName
$PackageFamilyName = $appx.PackageFamilyName $PackageFamilyName = $appx.PackageFamilyName
Run-Trusted -command "New-Item `"$store\Deprovisioned\$PackageFamilyName`" -force" New-Item "$store\Deprovisioned\$PackageFamilyName" -force
Start-Sleep .5
Run-Trusted -command "dism /online /set-nonremovableapppolicy /packagefamily:$PackageFamilyName /nonremovable:0" dism /online /set-nonremovableapppolicy /packagefamily:$PackageFamilyName /nonremovable:0
Start-Sleep .5
#remove inbox apps #remove inbox apps
$inboxApp = "$store\InboxApplications\$PackageFullName" $inboxApp = "$store\InboxApplications\$PackageFullName"
Run-Trusted -command "Remove-Item -Path $inboxApp -Force" Remove-Item -Path $inboxApp -Force
Start-Sleep .5
#get all installed user sids for package due to not all showing up in reg #get all installed user sids for package due to not all showing up in reg
foreach ($user in $appx.PackageUserInformation) { foreach ($user in $appx.PackageUserInformation) {
$sid = $user.UserSecurityID.SID $sid = $user.UserSecurityID.SID
if ($users -notcontains $sid) { if ($users -notcontains $sid) {
$users += $sid $users += $sid
} }
Run-Trusted -command "New-Item `"$store\EndOfLife\$sid\$PackageFullName`" -force" New-Item "$store\EndOfLife\$sid\$PackageFullName" -force
Start-Sleep .5 remove-appxpackage -package $PackageFullName -User $sid
Run-Trusted -command "remove-appxpackage -package $PackageFullName -User $sid"
Start-Sleep .5
} }
$eol += $PackageFullName $eol += $PackageFullName
Run-Trusted -command "remove-appxpackage -package $PackageFullName -allusers" remove-appxpackage -package $PackageFullName -allusers
} }
} }
'@
Set-Content -Path $packageRemovalPath -Value $code -Force
Write-Host 'Removing AI Appx Packages...'
$command = "&$env:TEMP\aiPackageRemoval.ps1"
Run-Trusted -command $command
#check packages removal
do {
Start-Sleep 1
$packages = get-appxpackage -AllUsers | Where-Object { $aipackages -contains $_.Name }
foreach ($package in $packages) {
if ($package.PackageUserInformation -like '*pending removal*') {
$ProgressPreference = 'SilentlyContinue'
&$env:TEMP\aiPackageRemoval.ps1 *>$null
}
}
}while ($packages)
#cleanup code
Remove-Item $packageRemovalPath -Force
## undo eol unblock trick to prevent latest cumulative update (LCU) failing ## undo eol unblock trick to prevent latest cumulative update (LCU) failing
foreach ($sid in $users) { foreach ($PackageName in $eol) { Remove-Item "$store\EndOfLife\$sid\$PackageName" -force -ErrorAction SilentlyContinue >'' } } foreach ($sid in $users) { foreach ($PackageName in $eol) { Remove-Item "$store\EndOfLife\$sid\$PackageName" -force -ErrorAction SilentlyContinue >'' } }
@@ -206,6 +243,7 @@ foreach ($sid in $users) { foreach ($PackageName in $eol) { Remove-Item "$store\
#remove recall optional feature #remove recall optional feature
$ProgressPreference = 'SilentlyContinue' $ProgressPreference = 'SilentlyContinue'
try { try {
Write-Host 'Removing Recall Optional Feature...'
Disable-WindowsOptionalFeature -Online -FeatureName 'Recall' -Remove -NoRestart -ErrorAction Stop *>$null Disable-WindowsOptionalFeature -Online -FeatureName 'Recall' -Remove -NoRestart -ErrorAction Stop *>$null
} }
catch { catch {