Posey's Tips & Tricks
Using PowerShell To Back Up Windows 11
A PowerShell script using WBAdmin can create full system backups of Windows 11, offering a simple, scriptable alternative for advanced users.
Over the last couple of years, I have written a few blog posts discussing various techniques for using PowerShell to create air gapped backups of your data. That's all well and good, but what if you want to create a "real" backup that includes more than just files and documents? It's relatively easy to use PowerShell to do exactly that.
Before I get started, there are a few things that you need to know. First, the script that I have created must be run in an elevated PowerShell session and you will need admin privileges. It's also worth noting that this script leverages Wbadmin, which is a core component of Windows Backup. As such, Windows Backup will need to be installed in order for this script to work. However, Windows Backup is usually installed by default on Windows 11 systems.
Finally, it probably goes without saying that I never intended for this script to replace your existing backup solution. I recommend using a reputable backup solution any time that you need to back up anything important. Likewise, you should test this script on a non-production system before using it.
So with that said, here is my script:
$BackupTarget = "E:\"
$VolumeToBackup = "C:"
$CurrentDate = Get-Date
$BackupName = "Backup-$CurrentDate"
# Make sure backup target exists
# Check if the file exists
if (-not (Test-Path -Path $BackupTarget)) {
Write-Host "The destination path $BackupTarget does not exist"
Exit
} else {
Write-Host "Volume will be backed up to: $BackupTarget"
}
# Perform the backup
$BackupString = "WbAdmin start backup -BackupTarget:$BackupTarget -Include:$VolumeToBackup -AllCritical -Quiet -VSSFull"
Write-Host $BackupString
Invoke-Expression $BackupString
Write-Host "Backup completed to $BackupTarget"
The script begins by defining a few parameters Specifically the script defines the volume that is to be backed up, the backup target, the current date and time, and the backup name (which is based on the date and time). The parameters used within the code instruct the script to backup the C: drive to E:
The next block of code is designed to verify that the backup target exists. The script uses the if (-not(Test-Path $BackupTarget)) command to verify the existence of the destination path. If the destination path (in this case, the E: drive) does not exist, then the script will display an error message and then terminate. If the destination path does exist, then the script will move forward with the backup.
Because my script uses variables to define the volume to be backed up and the backup target, I am not using a hard coded WBAdmin command. Instead, I am forming a command string, which is called $BackupString. This string includes the full WBAdmin command, including the parameters defined by variables. Once this command has been assembled, I am using the Invoke-Expression cmdlet to execute the string, thereby performing the backup. You can see what the backup process looks like in Figure 1.
[Click on image for larger view.]
Figure 1. This is what the backup process looks like.
The WBAdmin command includes the phrase "start backup," which tells WBAdmin that we want to perform a backup. Next, the command specifies the backup target and the volume to backup. Beyond this, there are three additional parameters that I am using.
The first of the additional parameters is the -AllCritical parameter. This tells WBAdmin that I want to include any volume containing operating system components. It is worth noting however, that if you use the -AllCritical parameter, you must also use the -BackupTarget parameter. Otherwise the command will fail.
Another parameter that I am using is -Quiet. This parameter allows the backup to run without the script prompting the end user.
Finally, the script is using the -VSSFull parameter. This parameter tells WBAdmin to use the Volume Shadow Copy Services when creating the backup. There is one critically important thing to know about using this parameter. You must not use the -VSSFull parameter if you sometimes use another backup application. There is a strong possibility that using the -VSSFull parameter will break any incremental or differential backups that you may be using. This happens because of the way that the parameter handles backup history and logfile truncation. If you want to run a scripted backup without impacting VSS, then either omit the -VSSFull switch or replace it with -VSSCopy. You can find Microsoft's full documentation of these parameters here.
About the Author
Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.