powershell script to remove backups older than 60 days

This commit is contained in:
Peter Wood
2023-11-23 11:13:51 -05:00
parent e51a7dd0e8
commit 3cfdb19a22

8
remove-files.ps1 Normal file
View File

@@ -0,0 +1,8 @@
$limit = (Get-Date).AddDays(-60)
$path = "\\peranda-nas\media\backups\docker-data"
# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse