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,55 +1993,64 @@ 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*") { if ($path -like "*$package*") {
$packagesPath += $path $packagesPath += $path
} }
} }
}
foreach ($path in $pathsWindowsApps) { $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
Handle = $rs.BeginInvoke()
} }
} }
foreach ($path in $pathsAppRepo) { $packagesPath += foreach ($job in $jobs) {
if ($path -like "*$package*") { $job.Runspace.EndInvoke($job.Handle)
$packagesPath += $path $job.Runspace.Dispose()
}
} }
foreach ($path in $pathsSXS) {
if ($path -like "*$package*") {
$packagesPath += $path
}
}
}
#get additional files
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) {
if ($path -like '*UserExperience-AIX*' -or $path -like '*Copilot*' -or $path -like '*UserExperience-Recall*' -or $path -like '*CoreAI*') {
$packagesPath += $path
}
}
#add app actions mcp host #add app actions mcp host
$paths = @( $paths = @(
@@ -2098,7 +2107,6 @@ 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
} }
@@ -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