Backing up Active Directory in Windows Server 2012 R2 with Powershell is now really easy thanks to the Windows Server Backup cmdlets provided in Powershell. Windows Server Backup allows you to create a Scheduled backup or a one time backup. In this example, I’ll be doing a one time backup but scheduling via a scheduled task to allow for more flexibility and I’ll be backing up the system state of the server.
The first thing that you will need to do if you haven’t done so already is to install the Windows Server Backup feature.
Add-Windowsfeature Windows-Server-Backup -Includeallsubfeature
Once that is done, below is a little script that I created for myself that will backup a server’s system state. If this is a domain controller, you could use the system state backup to restore Active Directory if needed.
#Create the backup folder target location first.
$backuplocation = Test-Path -Path '\\bc-core01\C$\backup'
New-Item -Path '\\bc-core01\C$\Backup' -ItemType Folder
##Check for an existing backup at Target Network Location
$oldbackup = Test-Path -Path '\\bc-core01\C$\Backup\WindowsImageBackup\BC-Server01\'
remove-item -Path '\\bc-core01\C$\Backup\WindowsImageBackup' -Recurse -Force
#Check for an existing Policy
$OldPolicy = Get-Wbpolicy
Remove-WBPolicy -Policy (Get-WBPolicy -editable)
#Create New Policy
$policy = New-WBpolicy
Add-WBsystemstate -Policy $policy
$BackupTargetNetwork=New-WBbackupTarget –NetworkPath \\bc-core01\C$\backup
Add-WBBackupTarget -Policy $policy -Target $BackupTargetNetwork
#Set the backup schedule
Start-WBBackup -Policy $Policy
Here are some screen caps of what it looks like when it is running.
To finish things off, you can then create a scheduled task to run the script at a time you would like.
For further information or to checkout the material I used to create this script please click on the following links: –
Windows Server Backup Cmdlets in Windows Powershell
Using Windows Server Backup Cmdlets
Windows Server Backup Step by Step Guide for Windows Server 2008 R2