Reputation: 353
I have a Powershell script that is supposed to delete items that are X days old. It doesnt fully work. It removes some files but not all of the files. When I run the script without | Remove-Item -Force
, all the files that meet the requirements are displayed. So, I know the where
statement works.
Why doesn't Remove-Item -Force
not delete all items that meet the requirements set be the where
statement, and how can it be fixed?
$deleteFiles = Get-Childitem $fullTargetPath -Recurse
| Where {$_.LastWriteTime -lt (Get-Date).AddDays(-10)} | Remove-Item -Force
Upvotes: 1
Views: 7648
Reputation: 6985
Just before the "Remove-Item" add "Foreach".
So for example:
$deleteFiles = Get-Childitem $fullTargetPath -Recurse |
Where {$_.LastWriteTime -lt (Get-Date).AddDays(-10)} |
Foreach { Remove-Item $_.FullName -Force}
Upvotes: 1