Powershell : Backing Up GPO Automatically with retention


This is a pretty simple script, but it enables the automatic backup of Of all your group policy objects, which we time stamped with the current day albeit in USA format, I find this helpful when you run this once a day in the morning.

The script will also only keep one months worth of backups so you don’t end up backing all your group policies over years.

Obviously, you can customize how long the backups are kept but I generally find one working month is sufficient.

Before running the script, ensure you have adequate access to be able to back up the group policy objects, and you should be able to set this to run every day and then forget it.

# Backup current GPO's to folder
Import-Module GroupPolicy
$date = Get-Date -Format M.d.yyyy
New-Item -Path C:\GPO-Backups\$date -ItemType directory
Backup-Gpo -All -Path C:\GPO-Backups\$date

# Perform cleanup of gpo-backups that are older than one month
Get-ChildItem C:\GPO-Backups\ | Where {($_.PSIsContainer) -and ($_.LastWriteTime -lt (date).AddMonths(-1))} | Remove-Item -Recurse -Force

Previous Post Next Post

نموذج الاتصال