- Local time
- 12:51 PM
- Posts
- 192
- OS
- Windows 11
Updates & running dism gives no errors. Also no eventvwr errors.
While doing updates on that testmachine, that was 2months behind on updates.
Did the updates, rebooted. Ran DISM /Online /Cleanup-Image /StartComponentCleanup and DISM /Online /Cleanup-Image /StartComponentCleanup /resetbase
rebooted again.
Then checked again for empty folders. There where 6346 new empty folders. I guess if this happens every month, then after 2years you get the 100k empty folder i see on some machines.
Reverted vm snapshot back to orginal state.
Took a shot with: @MyITGuy MyITGuy/Invoke-WinSxSTempCleanup.psm1
MyITGuy's is a few minutes faster in changing access rights and deleting folders.
But his script does not check if folders are empty, so deletes also the files inside some of the temp folders as well.
This is not my goal, as windows will release them by it self. (No where i found old folders with files in it, only from within the last few weeks.)
He has defined PendingDeletes and PendingRenames, but only writes a line, that there are files, and does nothing with it.
I will keep his script, as it might be a handy working example to change access rights in situations where the other take ownership scripts not work.
Made this script, so you can give up the path to scan to delete folders.
After the question -> Would you like to delete empty folders recursively until none are left? (yes/no)
It will delete all empty folders that are found.
Adjust scanPath with fixed path and adjust deleteEmptyFolders to Yes, to run this script automated as a scheduled task. Or create a function of it, to intergrate into excisting scripts that runs on your pc or networks.
Feedback is welcome!
While doing updates on that testmachine, that was 2months behind on updates.
Did the updates, rebooted. Ran DISM /Online /Cleanup-Image /StartComponentCleanup and DISM /Online /Cleanup-Image /StartComponentCleanup /resetbase
rebooted again.
Then checked again for empty folders. There where 6346 new empty folders. I guess if this happens every month, then after 2years you get the 100k empty folder i see on some machines.
Reverted vm snapshot back to orginal state.
Took a shot with: @MyITGuy MyITGuy/Invoke-WinSxSTempCleanup.psm1
MyITGuy's is a few minutes faster in changing access rights and deleting folders.
But his script does not check if folders are empty, so deletes also the files inside some of the temp folders as well.
This is not my goal, as windows will release them by it self. (No where i found old folders with files in it, only from within the last few weeks.)
He has defined PendingDeletes and PendingRenames, but only writes a line, that there are files, and does nothing with it.
I will keep his script, as it might be a handy working example to change access rights in situations where the other take ownership scripts not work.
Made this script, so you can give up the path to scan to delete folders.
After the question -> Would you like to delete empty folders recursively until none are left? (yes/no)
It will delete all empty folders that are found.
Adjust scanPath with fixed path and adjust deleteEmptyFolders to Yes, to run this script automated as a scheduled task. Or create a function of it, to intergrate into excisting scripts that runs on your pc or networks.
Feedback is welcome!
Powershell:
# Prompt user
$scanPath = Read-Host "Enter the path to scan (e.g. C:\Windows\WinSxS\Temp\InFlight)"
if (!(Test-Path -Path $scanPath)) {
Write-Host "The entered path does not exist. Please try again."
exit
}
$deleteEmptyFolders = Read-Host "Would you like to delete empty folders recursively until none are left? (yes/no)"
$totalDeletedFolderCount = 0
if ($deleteEmptyFolders -eq "yes") {
do {
$emptyFolders = Get-ChildItem -Path $scanPath -Recurse -Directory | Where-Object { (Get-ChildItem -Path $_.FullName).Count -eq 0 }
if ($emptyFolders.Count -eq 0) {
Write-Host "No more empty folders found."
break
}
$deletedFolderCount = 0
foreach ($folder in $emptyFolders) {
try {
takeown /f "$($folder.FullName)" /r /d y
icacls "$($folder.FullName)" /grant administrators:F /t
}
catch {
Write-Warning "Failed to take ownership or grant permissions for $($folder.FullName). Error: $($_.Exception.Message)"
continue
}
# remove the folder
try {
Remove-Item -Path "$($folder.FullName)" -Recurse -Force -Confirm:$false
$deletedFolderCount++
}
catch {
Write-Warning "Failed to delete folder $($folder.FullName). Error: $($_.Exception.Message)"
}
}
$totalDeletedFolderCount += $deletedFolderCount
} while ($true)
Write-Host "Total empty folders deleted across all iterations: $totalDeletedFolderCount"
} else {
Write-Host "Empty folders not deleted."
}
My Computer
System One
-
- OS
- Windows 11
- Computer type
- PC/Desktop