Question

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
Était-ce utile?

La solution

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}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top