PowerShell: Backup all GPO’s linked to an OU

I needed to take a backup of all the Group Policy Objects that were linked to a particular Organizational Unit… so I created a simple PowerShell script to do just that:

  • Create a backup folder based on date
  • Use the ‘Get-ADOrganizationalUnit’ cmdlet to list of  all of the GPO’s linked to the specified OU
  • For each of the GPO’s
    • Convert the GUID to GPO name
    • Create a subfolder based on GPO Name
    • Backup the GPO

Script below:

Import-Module -Name ActiveDirectory

# – Set Variables
$OU=”OU=Windows 10,OU=Clients,DC=LAB,DC=LOCAL”

# – Create Backup Folder
If (Test-Path $RootBackupFolder\$BackupFolder) {
Write-Verbose “Backup Folder already exists”
Else {
New-Item -Path $RootBackupFolder\$BackupFolder -ItemType Directory -Verbose

# – Main
# — Get all GPO’s linked to OU
(Get-ADOrganizationalUnit -Identity $OU).LinkedGroupPolicyObjects | % {
# — Get GPO details
$GPOGUID=”{” + ($_.Split(“{“)[1]).Split(“}”)[0] + “}”
$GPOName=(Get-GPO -Guid $GPOGUID).DisplayName
Write-Verbose “Processing: $GPOName”
# — Create backup folder based on GPO Name
New-Item -Path $RootBackupFolder\$BackupFolder\$GPOName -ItemType Directory -Verbose
# — Backup GPO
Backup-GPO -Guid $GPOGUID -Path $RootBackupFolder\$BackupFolder\$GPOName -Verbose

. Surj


Speak your mind...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s