<# .SYNOPSIS Backs up all GPOs while retaining a certain number of copies .DESCRIPTION Backs up all GPOs while retaining a certain number of copies .PARAMETER Path The Path parameter specifies the location where the GPOs will be saved * Example: C:\GPOBackup .PARAMETER DailyRetention The DailyRetention parameter specifies the number of daily backups to retain * Example: 14 .PARAMETER WeeklyRetention The WeeklyRetention parameter specifies the number of weekly backups to retain * Example: 6 .PARAMETER MonthlyRetention The MonthlyRetention parameter specifies the number of monthly backups to retain * Example: 12 .PARAMETER DayForMonthlyBackup The DayForMonthlyBackup parameter specifies the day of the month to take a monthly backup * Example: 1 .PARAMETER DayOfWeekForWeeklyBackup The DayOfWeekForWeeklyBackup parameter specifies the day of the week to take a weekly backup * Example: Sunday .EXAMPLE Backup-GpoWithRetention -Path e:\GPOBackup Description ----------- Backs up all GPOs into the e:\GPOBackup directory with default daily (14), weekly (6) and monthly (12) retentions Weekly backups taken by default on Sunday, and Monthly backups taken by default on the 1st of the month .NOTES Author: Twan van Beers, Nero Blanco IT Ltd #> Param( [Parameter(Position=0,Mandatory=$false)] [ValidateScript({Test-Path $_ -PathType 'Container'})] [string]$Path = "C:\GPOBackup", [Parameter(Position=1,Mandatory=$false)] [ValidateRange(0,365)] [int]$DailyRetention = 14, [Parameter(Position=2,Mandatory=$false)] [ValidateRange(0,104)] [int]$WeeklyRetention = 6, [Parameter(Position=3,Mandatory=$false)] [ValidateRange(0,60)] [int]$MonthlyRetention = 12, [Parameter(Position=4,Mandatory=$false)] [ValidateRange(0,28)] [int]$DayForMonthlyBackup = 1, [Parameter(Position=5,Mandatory=$false)] [ValidateScript({[system.dayofweek].getenumvalues() -contains $_})] [system.dayofweek]$DayOfWeekForWeeklyBackup = [system.dayofweek].getenumvalues()[0] ) $Today = get-date $Todaystring = $Today.tostring( 'yyyyMMdd' ) $DailyPath = "$Path\Daily\" $WeeklyPath = "$Path\Weekly\" $MonthlyPath = "$Path\Monthly\" $TodayDailyPath = "$DailyPath\$TodayString" $TodayWeeklyPath = "$WeeklyPath\$TodayString" $TodayMonthlyPath = "$MonthlyPath\$TodayString" ########################## DAILY ########################## if( !(Test-Path $DailyPath) ) { New-Item $DailyPath -Type Directory | out-null } if( (Test-Path $TodayDailyPath) ) { Remove-Item $TodayDailyPath -Force -Recurse } if( !(Test-Path $TodayDailyPath) ) { New-Item $TodayDailyPath -Type Directory | out-null } $error.clear() Get-GPO -All | Backup-GPO -Path $TodayDailyPath if( $error.count -eq 0 ) { Get-ChildItem $DailyPath | sort -Descending name | select -Skip $DailyRetention | Remove-Item -Recurse -Force ########################## WEEKLY ########################## if( $today.DayOfWeek -eq $DayOfWeekForWeeklyBackup ) { if( !(Test-Path $WeeklyPath) ) { New-Item $WeeklyPath -Type Directory | out-null } if( (Test-Path $TodayWeeklyPath) ) { Remove-Item $TodayWeeklyPath -Force -Recurse } Copy-Item $TodayDailyPath -Destination $TodayWeeklyPath -Recurse -Force Get-ChildItem $WeeklyPath | sort -Descending name | select -Skip $WeeklyRetention | Remove-Item -Recurse -Force } ########################## MONTHLY ########################## if( $today.Day -eq $DayForMonthlyBackup ) { if( !(Test-Path $MonthlyPath) ) { New-Item $MonthlyPath -Type Directory | out-null } if( (Test-Path $TodayMonthlyPath) ) { Remove-Item $TodayMonthlyPath -Force -Recurse } Copy-Item $TodayDailyPath -Destination $TodayMonthlyPath -Recurse -Force Get-ChildItem $MonthlyPath | sort -Descending name | select -Skip $MonthlyRetention | Remove-Item -Recurse -Force } }