Reputation: 722
I'm creating a script that, among the tasks, deletes log files with more than 10 days. But the files are not being deleted. Follow the code snippet
$LOCAL_BKP = 'E:\prod'
$QTD_DIAS = "-10"
$BACKUP_DATE = Get-Date -UFormat "%Y-%m-%d_%H%M%S"
$ARQUIVO_LOG = "$($LOCAL_BKP)\log_backup_$($BACKUP_DATE).log"
Get-ChildItem –Path $LOCAL_BKP -Include *.log | Where-Object {$_.LastWriteTime –lt (Get-Date).AddDays($QTD_DIAS)} | Remove-Item -Recurse | Out-File -Append -NoClobber -filepath $ARQUIVO_LOG
Upvotes: 1
Views: 68
Reputation: 13537
You're running into this issue because of the way the -Include
parameter works for Get-ChildItem
. From the help:
-Include <String[]>
Gets only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as
"*.txt". Wildcards are permitted.
**The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a
directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.**
Note the second part. -Include
only functions if you specify -Recurse
or specify * for the path of a directory.
If you change your commandline to this:
Get-ChildItem –Path $LOCAL_BKP\*.log | Where-Object {$_.LastWriteTime –lt (Get-Date).AddDays($QTD_DIAS)}
It should work as expected. If it still doesn't, make sure that you do have files older than 10 days present.
Upvotes: 1