Automated Hyper-V VM Backup Script

This PowerShell script automates the process of backing up your Hyper-V virtual machines. It simplifies the backup routine, ensuring your data is safe and easy to restore. For more powerful tools and scripts, check out ServerEngine at https://serverengine.co.
Step 1: Define the backup directory where the VM files will be stored.

$backupDirectory = "C:\HyperVBackups"

Step 2: Ensure the backup directory exists; if not, create it.

if (-Not (Test-Path -Path $backupDirectory)) {
    New-Item -ItemType Directory -Path $backupDirectory
}

Step 3: Get all Hyper-V virtual machines and loop through them to back them up.

$vms = Get-VM
foreach ($vm in $vms) {
    $sourcePath = $vm.Path
    $destinationPath = Join-Path -Path $backupDirectory -ChildPath "$($vm.Name).vhdx"
    Copy-Item -Path "$sourcePath\*.vhdx" -Destination $destinationPath -Recurse
}

Step 4: Output a message indicating the backup process is complete.

Write-Host "Backup of Hyper-V VMs completed successfully to $backupDirectory"

This script helps automate your VM backup process, ensuring you have regular backups of your Hyper-V environment. Customize as needed to fit your backup strategy!