| 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 |
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]
[!INCLUDE updated-for-az]
[!code-powershellmain]
Run the following command to remove the resource group, VM, and all related resources.
Remove-AzResourceGroup -Name myResourceGroupThis 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. |
Learn more about the Azure PowerShell module.