create-scheduled-task-to-purge-old-files.ps1 407 B

1234567
  1. $action = New-ScheduledTaskAction -Execute 'Powershell.exe' `
  2. -Argument '-Command "Get-ChildItem -Path F:\Backup -File -Recurse -Force | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-7))}| Remove-Item -Force"'
  3. $trigger = New-ScheduledTaskTrigger -Daily -At 9am
  4. Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "Purge old backup" -Description "Purge files older than 1 week"