Add Windows/removeFilesOlderThan.ps1

This commit is contained in:
2023-12-19 15:36:12 +00:00
parent bfe36f0d4f
commit d1568cc0c3

View File

@@ -0,0 +1,8 @@
$limit = (Get-Date).AddDays(-30)
$path = "\\NETWORKSHARE"
# 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