Headlines

In an organization it is very crucial to manage users and computer according to company requirement. Backing up of the servers and data are also important as much as control. Domain controller provides group policy object feature in order to manage computer and user within the organization in the context of security and control.

In this article you will earn how to backup group policy object in a specified location using PowerShell. in this example i have used my domain server name mohammed.com.np and backup location at C:\\GPO backup to execute this PowerShell script.

# Date: 11-2024
# Summary: Automatize the backup process of GPO for a domain.
# Create a folder with the time when this script was executed and inside it a folder for each GPO of the domain
# The program needs two mandatory parameters: 
#  * Domain: The domain from where we want to backup the GPOs
#  * backuppath: The path to the folder where we want to store this backups

param(
    [Parameter(Mandatory=$True,Position=1)][String]$domain,
    [Parameter(Mandatory=$True,Position=2)][String]$backuppath
)

import-module grouppolicy 

$today = Get-Date -Format "yyyy-MM-dd_HHmmss"

# Check if the backup path exists
if (-not (Test-Path($backuppath))) {
    $r = Read-Host -Prompt "$backuppath does not exists, do you want to create it and continue? [y/N]: "
    if ($r-eq "y" -or $r -eq "Y"){
        New-Item -ItemType directory $backuppath > $null
    }
    else {
        # The script stops
        Write-Host "User stoped the script execution. Exiting now."
        exit
    }
}

# Create the folder for today backup
$dstfolder = $backuppath+"\"+$today
New-Item -ItemType directory $dstfolder > $null

if (Test-Path $dstfolder){
	write-host "The GPOs will be saved in the folder $dstfolder"
}
else {
	write-host -foregroundcolor "red" "There was some error creating the folder $dstfolder. Check your permisions and the existence of the path. Exiting now"
	exit 1
}

$policiesnames = get-gpo -All  -Domain $domain | select DisplayName

# create the backup folder structure
$policiesnames | ForEach-Object {
    $polname = $_.DisplayName
    # Backup each gpo inside its folder
    $gpofolder = $dstfolder+"\$polname"
    Write-Host " Backing up in $gpofolder"
    New-Item -ItemType directory -Path $gpofolder > $null
    Backup-GPO -Name "$polname" -Path $gpofolder -Comment "$polname" -Domain $domain > $null
}



copy and paste the above code in PowerShell edit and just execute this.

Enter your domain name and backup location as specified below.

your all group policy objects are backed up by the script. now just verify the group are in the location we provided in the script.

Here, You are at end of this article , now you might be interested to learn how to restore the Group policy objects which are already backed up in the previous step.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top