Set-AzVM
This cmdlet modifies a virtual machine in Azure.
Syntax
Set-AzVM
[-VM] <PSVirtualMachine>
[-ResourceGroupName <String>]
[-Name <String>]
[-DisableVhdBackup <Boolean>]
[-VhdBackup <Boolean>]
[-ForceDeletion <Boolean>]
[-AsJob]
[-DefaultProfile <IAzureContextContainer>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
The Set-AzVM cmdlet modifies a virtual machine. You can use this cmdlet to perform operations such as changing the size of a virtual machine, attaching or detaching data disks, or setting the backup policy for a virtual machine.
To use the cmdlet, you first create a virtual machine object, modify its properties, and then pass the object to Set-AzVM.
Parameters
| Name | Type | Description |
|---|---|---|
| -VM | Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine | Specifies the virtual machine object to modify. |
| -ResourceGroupName | System.String | The name of the resource group for the virtual machine. |
| -Name | System.String | The name of the virtual machine. |
| -DisableVhdBackup | System.Boolean | Disable VHD backup for the virtual machine. |
| -VhdBackup | System.Boolean | Enable VHD backup for the virtual machine. |
| -ForceDeletion | System.Boolean | Perform the operation without prompting for confirmation. |
| -AsJob | System.Management.Automation.SwitchParameter | Run cmdlet in the background. |
| -DefaultProfile | Microsoft.Azure.Commands.Common.Authentication.Abstractions.IAzureContextContainer | The credentials, account, tenant, and subscription for the current session. |
| -WhatIf | System.Management.Automation.SwitchParameter | Shows what would happen if the cmdlet runs. The cmdlet is not run. |
| -Confirm | System.Management.Automation.SwitchParameter | Prompts you for confirmation before running the cmdlet. |
Examples
Example 1: Change the size of a virtual machine
# Load the module
Import-Module Az.Compute
# Get the virtual machine object
$vm = Get-AzVM -Name "MyVM" -ResourceGroupName "MyResourceGroup"
# Specify the new VM size
$vm.HardwareProfile.VmSize = "Standard_D2s_v3"
# Update the virtual machine
Set-AzVM -VM $vm
Write-Host "Virtual machine size updated successfully."
Example 2: Attach a data disk to a virtual machine
# Load the module
Import-Module Az.Compute
# Get the virtual machine object
$vm = Get-AzVM -Name "MyVM" -ResourceGroupName "MyResourceGroup"
# Define the new data disk configuration
$diskConfig = New-AzVMDataDisk -Name "DataDisk1" -DiskSizeGB 100 -Lun 0 -CreateOption Empty
# Add the data disk to the VM object
Add-AzVMDataDisk -VM $vm -VMDataDisk $diskConfig
# Update the virtual machine with the new data disk
Set-AzVM -VM $vm
Write-Host "Data disk attached successfully."