use runspaces to speed up removing appx package files

This commit is contained in:
zoicware
2026-03-17 20:09:19 -04:00
committed by GitHub
parent 077196d019
commit 477e48ee44

View File

@@ -1993,56 +1993,65 @@ function Remove-AI-Files {
if (!(Test-Path $appsPath6)) { if (!(Test-Path $appsPath6)) {
$appsPath6 = "$env:windir\SystemApps\SxS" $appsPath6 = "$env:windir\SystemApps\SxS"
} }
$pathsSystemApps = (Get-ChildItem -Path $appsPath -Directory -Force -ErrorAction SilentlyContinue).FullName
$pathsWindowsApps = (Get-ChildItem -Path $appsPath2 -Directory -Force -ErrorAction SilentlyContinue).FullName $paths = @(
$pathsAppRepo = (Get-ChildItem -Path $appsPath3 -Directory -Force -Recurse -ErrorAction SilentlyContinue).FullName $appsPath,
$pathsServicing = (Get-ChildItem -Path $appsPath4 -Directory -Force -Recurse -ErrorAction SilentlyContinue).FullName $appsPath2,
$pathsCatRoot = (Get-ChildItem -Path $appsPath5 -Directory -Force -Recurse -ErrorAction SilentlyContinue).FullName $appsPath3,
$pathsSXS = (Get-ChildItem -Path $appsPath6 -Directory -Force -ErrorAction SilentlyContinue).FullName $appsPath6
)
$jobs = foreach ($path in $paths) {
$rs = [powershell]::Create().AddScript({
param($path)
(Get-ChildItem -Path $path -Directory -Force -ErrorAction SilentlyContinue).FullName
}).AddParameter('path', $path)
[pscustomobject]@{
Runspace = $rs
Handle = $rs.BeginInvoke()
}
}
$fullPaths = foreach ($job in $jobs) {
$job.Runspace.EndInvoke($job.Handle)
$job.Runspace.Dispose()
}
$packagesPath = @() $packagesPath = @()
#get full path
foreach ($package in $aipackages) { foreach ($package in $aipackages) {
foreach ($path in $fullPaths) {
foreach ($path in $pathsSystemApps) {
if ($path -like "*$package*") {
$packagesPath += $path
}
}
foreach ($path in $pathsWindowsApps) {
if ($path -like "*$package*") {
$packagesPath += $path
}
}
foreach ($path in $pathsAppRepo) {
if ($path -like "*$package*") { if ($path -like "*$package*") {
$packagesPath += $path $packagesPath += $path
} }
} }
}
foreach ($path in $pathsSXS) { $paths = @($appsPath4, $appsPath5)
if ($path -like "*$package*") { $jobs = foreach ($path in $paths) {
$packagesPath += $path $rs = [powershell]::Create().AddScript({
} param($path)
} (Get-ChildItem -Path $path -Directory -Force -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like '*UserExperience-AIX*' -or
$_.FullName -like '*Copilot*' -or
$_.FullName -like '*UserExperience-Recall*' -or
$_.FullName -like '*CoreAI*'
}).FullName
}).AddParameter('path', $path)
} [pscustomobject]@{
Runspace = $rs
#get additional files Handle = $rs.BeginInvoke()
foreach ($path in $pathsServicing) {
if ($path -like '*UserExperience-AIX*' -or $path -like '*Copilot*' -or $path -like '*UserExperience-Recall*' -or $path -like '*CoreAI*') {
$packagesPath += $path
} }
} }
foreach ($path in $pathsCatRoot) { $packagesPath += foreach ($job in $jobs) {
if ($path -like '*UserExperience-AIX*' -or $path -like '*Copilot*' -or $path -like '*UserExperience-Recall*' -or $path -like '*CoreAI*') { $job.Runspace.EndInvoke($job.Handle)
$packagesPath += $path $job.Runspace.Dispose()
}
} }
#add app actions mcp host #add app actions mcp host
$paths = @( $paths = @(
"$env:LOCALAPPDATA\Microsoft\WindowsApps\ActionsMcpHost.exe" "$env:LOCALAPPDATA\Microsoft\WindowsApps\ActionsMcpHost.exe"
@@ -2098,10 +2107,9 @@ function Remove-AI-Files {
} }
$command = "Remove-item ""$Path"" -force -recurse" $command = "Remove-item ""$Path"" -force -recurse"
Run-Trusted -command $command -psversion $psversion Run-Trusted -command $command -psversion $psversion
Start-Sleep 1
} }
#remove machine learning dlls #remove machine learning dlls
$paths = @( $paths = @(
"$env:SystemRoot\System32\Windows.AI.MachineLearning.dll" "$env:SystemRoot\System32\Windows.AI.MachineLearning.dll"
@@ -3950,6 +3958,7 @@ else {
'WebViewHost.exe' 'WebViewHost.exe'
'aimgr.exe' 'aimgr.exe'
'AppActions.exe' 'AppActions.exe'
'M365Copilot.exe'
) )
foreach ($procName in $aiProcesses) { foreach ($procName in $aiProcesses) {
taskkill /im $procName /f *>$null taskkill /im $procName /f *>$null