Files
Boilerplates/Windows/BackupGPO.ps1
2023-12-19 15:35:45 +00:00

10 lines
580 B
PowerShell

$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
Backup-GPO -All -path $path -Comment "Backup done from SERVERNAME"