Toastie 5 months ago
parent
commit
7f0c8a6365
1 changed files with 7 additions and 0 deletions
  1. 7 0
      microsoft/powershell/create-scheduled-task-to-purge-old-files.ps1

+ 7 - 0
microsoft/powershell/create-scheduled-task-to-purge-old-files.ps1

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