Set the resource group and VM name variables. Replace the values with information of the VM you want to resize.
$resourceGroup = "myResourceGroup"
$vmName = "myVM"
List the VM sizes that are available on the hardware cluster where the VM is hosted.
Get-AzVMSize -ResourceGroupName $resourceGroup -VMName $vmName
Resize the VM to the new size.
$vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName
$vm.HardwareProfile.VmSize = "<newAv2VMsize>"
Update-AzVM -VM $vm -ResourceGroupName $resourceGroup
Use PowerShell to resize a VM not in an availability set.
This Cloud shell PowerShell script initializes the variables $resourceGroup
, $vm
, and $size
with the resource group name, VM name, and desired VM size respectively. It then retrieves the VM object from Azure using the Get-AzVM
cmdlet. The script modifies the VmSize
property of the VM's hardware profile to the desired size. Finally, it applies these changes to the VM in Azure using the Update-AzVM
cmdlet.
$resourceGroup = 'myResourceGroup'
$vmName = 'myVM'
$size = 'Standard_DS3_v2'
$vm = Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName
$vm.HardwareProfile.VmSize = $size
Update-AzVM -ResourceGroupName $resourceGroup -VM $vm
As an alternative to running the script in Azure Cloud Shell, you can also execute it locally on your machine. This local version of the PowerShell script includes additional steps to import the Azure module and authenticate your Azure account.
Note
The local PowerShell may require the VM to restart to take effect.
Import-Module Az
Connect-AzAccount
$resourceGroup = 'myResourceGroup'
$vmName = 'myVM'
$size = 'Standard_DS3_v2'
Select-AzSubscription -SubscriptionId '<subscriptionID>'
$vm = Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName
$vm.HardwareProfile.VmSize = $size
Update-AzVM -ResourceGroupName $resourceGroup -VM $vm
Babala
Deallocating the VM also releases any dynamic IP addresses assigned to the VM. The OS and data disks are not affected.
If you are resizing a production VM, consider using Azure Capacity Reservations to reserve Compute capacity in the region.
Use PowerShell to resize a VM in an availability set
If the new size for a VM in an availability set isn't available on the hardware cluster currently hosting the VM, then you need to deallocate all VMs in the availability set to resize the VM. You also might need to update the size of other VMs in the availability set after one VM has been resized. To resize a VM in an availability set, run the below script. You can replace the values of $resourceGroup
, $vmName
, $newVmSize
, and $availabilitySetName
with your own.
$resourceGroup = "myResourceGroup"
$vmName = "myVM"
$newVmSize = "<newVmSize>"
$availabilitySetName = "<availabilitySetName>"
$availableSizes = Get-AzVMSize `
-ResourceGroupName $resourceGroup `
-VMName $vmName |
Select-Object -ExpandProperty Name
if ($availableSizes -notcontains $newVmSize) {
$as = Get-AzAvailabilitySet `
-ResourceGroupName $resourceGroup `
-Name $availabilitySetName
$virtualMachines = $as.VirtualMachinesReferences | Get-AzResource | Get-AzVM
$virtualMachines | Stop-AzVM -Force -NoWait
$virtualMachines | Foreach-Object { $_.HardwareProfile.VmSize = $newVmSize }
$virtualMachines | Update-AzVM
$virtualMachines | Start-AzVM
exit
}
$vm = Get-AzVM `
-ResourceGroupName $resourceGroup `
-VMName $vmName
$vm.HardwareProfile.VmSize = $newVmSize
Update-AzVM `
-VM $vm `
-ResourceGroupName $resourceGroup
This script sets the variables $resourceGroup
, $vmName
, $newVmSize
, and $availabilitySetName
. It then checks if the desired VM size is available by using Get-AzVMSize
and checking if the output contains the desired size. If the desired size isn't available, the script deallocates all VMs in the availability set, resizes them, and starts them again. If the desired size is available, the script resizes the VM.
To resize a VM, you need the latest Azure CLI installed and logged in to an Azure account using az sign-in.
The below script checks if the desired VM size is available before resizing. If the desired size isn't available, the script exits with an error message. If the desired size is available, the script deallocates the VM, resizes it, and starts it again. You can replace the values of resourceGroup
, vm
, and size
with your own.
resourceGroup=myResourceGroup
vm=myVM
size=Standard_DS3_v2
if ! az vm list-vm-resize-options --resource-group $resourceGroup --name $vm --query "[].name" | grep -q $size; then
echo "The desired VM size is not available."
exit 1
fi
az vm deallocate --resource-group $resourceGroup --name $vm
az vm resize --resource-group $resourceGroup --name $vm --size $size
az vm start --resource-group $resourceGroup --name $vm
Babala
Deallocating the VM also releases any dynamic IP addresses assigned to the VM. The OS and data disks are not affected.
If you are resizing a production VM, consider using Azure Capacity Reservations to reserve Compute capacity in the region.
Use Azure CLI to resize a VM in an availability set.
The below script sets the variables resourceGroup
, vm
, and size
. It then checks if the desired VM size is available by using az vm list-vm-resize-options
and checking if the output contains the desired size. If the desired size isn't available, the script exits with an error message. If the desired size is available, the script deallocates the VM, resizes it, and starts it again.
resourceGroup="myResourceGroup"
vmName="myVM"
newVmSize="<newVmSize>"
availabilitySetName="<availabilitySetName>"
availableSizes=$(az vm list-vm-resize-options \
--resource-group $resourceGroup \
--name $vmName \
--query "[].name" \
--output tsv)
if [[ ! $availableSizes =~ $newVmSize ]]; then
vmIds=$(az vmss list-instances \
--resource-group $resourceGroup \
--name $availabilitySetName \
--query "[].instanceId" \
--output tsv)
az vm deallocate \
--ids $vmIds \
--no-wait
az vmss update \
--resource-group $resourceGroup \
--name $availabilitySetName \
--set virtualMachineProfile.hardwareProfile.vmSize=$newVmSize
az vmss start \
--resource-group $resourceGroup \
--name $availabilitySetName \
--instance-ids $vmIds
exit
fi
az vm resize \
--resource-group $resourceGroup \
--name $vmName \
--size $newVmSize