Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 3.52 KB

File metadata and controls

53 lines (38 loc) · 3.52 KB
title PowerShell Script Sample - Back up an Azure VM
description In this article, learn how to use an Azure PowerShell Script sample to back up an Azure virtual machine.
ms.topic sample
ms.date 04/30/2025
ms.custom mvc, devx-track-azurepowershell
author AbhishekMallick-MS
ms.author v-mallicka

Back up an encrypted Azure virtual machine with PowerShell

This script creates a Recovery Services vault with geo-redundant storage (GRS) for an encrypted Azure VM. It applies the default protection policy, enabling daily backups retained for 365 days. Also, it triggers an initial recovery point, stored for 30 days.

[!INCLUDE sample-powerShell-install]

[!INCLUDE quickstarts-free-trial-note]

Sample script

[!INCLUDE updated-for-az]

[!code-powershellmain]

Clean up deployment

Run the following command to remove the resource group, VM, and all related resources.

Remove-AzResourceGroup -Name myResourceGroup

Script explanation

This script uses the following commands to create the deployment. Each item in the table links to command specific documentation.

Command Notes
New-AzResourceGroup Creates a resource group in which all resources are stored.
New-AzRecoveryServicesVault Creates a Recovery Services vault to store backups.
Set-AzRecoveryServicesBackupProperty Sets backup storage properties on Recovery Services vault.
New-AzRecoveryServicesBackupProtectionPolicy Creates protection policy using schedule policy and retention policy in Recovery Services vault.
Set-AzKeyVaultAccessPolicy Sets permissions on the Key Vault to grant the service principal access to encryption keys.
Enable-AzRecoveryServicesBackupProtection Enables backup for an item with a specified Backup protection policy.
Set-AzRecoveryServicesBackupProtectionPolicy Modifies an existing Backup protection policy.
Backup-AzRecoveryServicesBackupItem Starts a backup for a protected Azure Backup item that isn't tied to the backup schedule.
Wait-AzRecoveryServicesBackupJob Waits for an Azure Backup job to finish.
Remove-AzResourceGroup Removes a resource group and all resources contained within.

Next steps

Learn more about the Azure PowerShell module.