Today we continue our series that answers common questions, or questions that have been asked about/of PowerShell. With Backup GPO Settings we intend to provide is one of the many, many ways you can accomplish these same tasks within PowerShell.
All of the solutions are ours and demonstrate the author’s skill and ability level at the time of writing. That is to say, we might not always write the best PowerShell code. However, if you know of a better way we welcome that input.
Let’s start. The question is:
Is there an easy way using PowerShell to backup all GPO settings?
The Answer:
Yes, yes there is. Microsoft has added in their RSAT tool and on Windows Servers a group policy module for PowerShell. Using this module the cmdlet is Backup-GPO. Now I am not going to leave you with just that, of course. If you need to back up the data then you need to backup to a directory. We also might as well name that directory today’s date so we can easily pick it out later should we need to.
Using the new script created to make PowerShell templates faster and easier the setup process only took a few seconds.
The first variable is $date
. To define this we will use the cmdlet get-date
, encapsulated and format the output .ToString
specifying Mdy
as the output format.
The next variable we define is the $path
. Where will the output go. We append the $date
variable to the end of the path name.
The if statement says if the directory of $path is not there, then make a new directory with the details from $path. That way we can save the files to the directory and easily look them up later. We are also verifying no data is being overwritten by doing this.
Lastly we use the Backup-GPO
cmdlet with the -All
modifier and for -Path
we use the $path
variable.