Powershell script to backup ALL Group Policy Objects to a network share complete with email notification.

# import the Group Policy module
if (-not (Get-Module GroupPolicy)){
	Import-Module GroupPolicy -ErrorAction Stop            
# remove backups older than 7 days
$max_days = "-7"
# get the current date
$curr_date = Get-Date
# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)

# set the backup path
$backupRoot = "\\server.contoso.com\backups\group-policy"

# set the email options
$smtpServer = 'mail.contoso.com'
$smtpPort = '25'
$fromAddy = '[email protected]'
$toAddy = '[email protected]'
$mailMsg = "GPO Backup for $curr_date complete. Backups saved in $backupRoot\$((get-date).toString('MM-dd-yyyy'))"
$mailSubject = "GPO Backup $curr_date"
# create the folder for todays date
md "$backupRoot\$((get-date).toString('MM-dd-yyyy'))"

# backup the GPOs
Backup-Gpo -All -Path "$backupRoot\$((get-date).toString('MM-dd-yyyy'))"

# delete the files
Get-ChildItem $backupRoot -Recurse | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item

# send an email stating it was backed up
Send-MailMessage -SmtpServer $smtpServer -From $fromAddy -To $toAddy -Body $mailMsg -Subject $mailSubject