If you would like to automate your Windows Server Backup workflows, you can use the following Powershell snippet.
1. Install Windows feature (if not already installed)
Firstly check if the Windows Server Backup feature is installed on the Windows Server machine. The easiest way is to execute the following statement in a Powershell Window which will install the feature if it is not installed yet.
Install-WindowsFeature Windows-Server-Backup
Note: if the feature is already installed you will see the following output stating “NoChangeNeeded”, which is OK.
Success Restart Needed Exit Code Feature Result ------- -------------- --------- -------------- True No NoChangeNeeded {}
2. Run following snippet
Run the snippet below to run a Windows Server Backup. This script contains comments explaining the various steps.
# ---------------------------------------------------------- # Check if Powershell is opened with Administrator rights. # If not, re-open this script asking for Administrator rights. # ---------------------------------------------------------- param([switch]$Elevated) function Test-Admin { $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) } if ((Test-Admin) -eq $false) { if ($elevated) { # tried to elevate, did not work, aborting } else { Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) } exit } # ---------------------------------------------------------- # Install Windows feature if it isn't installed already.. Install-WindowsFeature Windows-Server-Backup $Policy = New-WBPolicy # Option 1: Enable the line below to backup the current system state. This includes many things such as installed device drivers, # most of the Windows Directory, the Windows Registry, the Active Directory config (where applicable) etc. Add-WBSystemState $Policy # Option 2: the command below will include the system volume (C partition) to the backup to enable a full server restore # Only enable this line if this is what you want. Add-WBBareMetalRecovery $Policy # Option 3: enable the line below to specify the locations manually that you want to backup (e.g. a single partition or folder) # You can duplicate this line to include multiple locations in the backup. # Specify the path of files/folders/volumes which we want to backup New-WBFileSpec -FileSpec "D:\MyApplication" | Add-WBFileSpec -Policy $Policy # Destination where the backup will be saved to (when specifying "D:\" the full output dir will defaults to D:\WindowsImageBackup) $BackupLocation = New-WBBackupTarget -VolumePath "D:\" Add-WBBackupTarget -Policy $Policy -Target $BackupLocation Set-WBVssBackupOptions -Policy $Policy -VssCopyBackup # Print configured settings $Policy # Wait for confirmation of user.. Write-Output "Check settings above and confirm these are correct. Press any key to start the backup.." Read-Host Write-Output "-----------------------------------------" Write-Output "Starting backup process, this might take some time..." Write-Output "-----------------------------------------" # Start backup process Start-WBBackup -Policy $Policy
You can read more about the various options in the Microsoft documentation at: https://docs.microsoft.com/en-us/powershell/module/windowsserverbackup/?view=windowsserver2022-ps
Good luck!