Skip to content

Instantly share code, notes, and snippets.

@ox-b
Forked from felmoltor/Backup-GPOs.ps1
Created June 5, 2019 21:12
Show Gist options
  • Save ox-b/a361cbbcaef5e820677bf5a647623fb5 to your computer and use it in GitHub Desktop.
Save ox-b/a361cbbcaef5e820677bf5a647623fb5 to your computer and use it in GitHub Desktop.
Powershell script to backup a domain gpo list
# Date: 10-2015
# Author: Felipe Molina (@felmoltor)
# Summary: Authomatize the backup proccess 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment