I have directory with about 12k folders with multiple subfolders inside.
I'm trying to remove all empty folders older than defined days, but the thing is not to remove folder (which last write time matches the limit), which has "younger" subfolder inside.
Using one of (many found), examples (which I modified a bit), I got really close, but unfortunately it got rid of prepared folder also (I created file in test folder to check if it will be deleted).
It appears that it doesn't really go through all sub folders.
Here is code:
$limit = (Get-Date).AddDays(-180)
$path = "path"
Get-ChildItem -Path $path -Recurse -Force | Where-Object {
($_.PSIsContainer -and $_.LastWriteTime -lt $limit) -and
(Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object {
!$_.PSIsContainer
}) -eq $null
} | Remove-Item -Force -Recurse
Any ideas what am I doing wrong with it?