Resizing a virtual machine (VM) in Azure is a simple feat if you know your PowerShell. Luckily, in this article, I'll be showing you how to create a PowerShell script to resize your Azure VMs that are either in an availability set or not.

First, to manage anything in Azure with PowerShell, you'll need to get your hands on the Az PowerShell module. To do this, run Install-Module Az. This will download the module and make it available in your PowerShell session. I'm also going to assume you have an Azure VM either in or not in an availability set. With that out of the way, let's look at how to resize Azure VMs with PowerShell!

Listing available sizes ^

Before we can resize a VM, we must first know which sizes are available. To do this, we can run the Get-AzVmSize command against the VM in question. I'll be working with a VM called CLIENT in this example. CLIENT is in the LAB resource group.

Get-AzVMSize -ResourceGroupName 'LAB' -VMName CLIENT
Listing VM sizes

Listing VM sizes

Resizing a VM not in an availability set ^

You can see we've got a lot of sizes to choose from. If you see a size you'd like, first create an instance of the VM using Get-AzVm.

$vm = Get-AzVM -ResourceGroupName 'LAB' -VMName 'CLIENT'

Next, change the VmSize property on that VM instance to the new size. In my case, I'll randomly pick the Standard_D3 size.

$vm.HardwareProfile.VmSize = 'Standard_D3'

Once I've saved the VM instance variable with the new size in my PowerShell session, it's then time to send that change up to Azure by running Update-AzVM and providing the VM instance and the resource group name the VM is in.

Update-AzVM -VM $vm -ResourceGroupName 'LAB'

This may take a bit to run as it does not work in the background. If you wanted to run this command in the background, you have the option of using the AsJob parameter.

Once this is complete, you should see an output like the one below:

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK

At this point, we can now check the VM size again by inspecting the VmSize property on the HardwareProfile object that's part of the VM instance object.

PS> (Get-AzVM -ResourceGroupName 'LAB' -VMName 'CLIENT').HardwareProfile.VmSize


Resizing a VM in an availability set ^

Finally, we can also resize VMs in an availability set using a similar approach. If a VM is in an availability set and the size you need is not available when running Get-AzVmSize, you can still change the size if you shut down all VMs in the availability set, update the sizes of all VMs, and then start up the VMs again.

We'll first find all of the VMs in the availability set and stop them all.

$as = Get-AzAvailabilitySet -ResourceGroupName 'LAB'
$vmIds = $as.VirtualMachinesReferences
foreach ($vmId in $vmIDs){
    $string = $vmID.Id.Split("/")
    $vmName = $string[8]
    Stop-AzVM -ResourceGroupName 'LAB' -Name $vmName -Force

After stopping all the VMs, we can now use the same method we used earlier for updating the VMs—but this time we update all the VMs in the availability set.

Subscribe to 4sysops newsletter!

$as = Get-AzAvailabilitySet -ResourceGroupName 'LAB'
$vmIds = $as.VirtualMachinesReferences
foreach ($vmId in $vmIDs){
    $string = $vmID.Id.Split("/")
    $vmName = $string[8]
    $vm = Get-AzVM -ResourceGroupName 'LAB' -Name $vmName
    $vm.HardwareProfile.VmSize = 'Standard_D3'
    Update-AzVM -ResourceGroupName 'LAB' -VM $vm
Start-AzVM -ResourceGroupName 'LAB' -Name $vmName


  1. In the current state, Microsoft Resize solution is not completely Elastic in cloud computing sense.
    Azure Datacentre is organized into multiple Stamp units. Every stamp unit has the hardware of similar configuration and supports a certain list of virtual machine sizes only. Resize or vertical scaling operation is scoped by this list of supported vm sizes in current stamp unit vm is hosted in.
    Even the Redeploy machine provisions the vm to one of the node within the stamp.
    As a workaround for this limitation, we have to create a new vm from the disks from the previous VMs requiring downtime for migration. 

  2. Ali Razeghi 3 years ago

    Thank you!

Leave a reply to Swapnil Kambli (Rank 3) Click here to cancel the reply

Please enclose code in pre tags

Your email address will not be published.


© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account