Add Disk Space to an Azure Virtual Machine Using Azure CLI

In this blog post, I will show you how I increase the size of my Microsoft Azure Windows Virtual Machine using Azure CLI.

Azure CLI is Microsoft Azure command-line tool that helps us manage and deploy resources on Microsoft Azure using commands.

Get Started

To get started, I will connect to Microsoft Azure using Azure Cloud Shell (shell.azure.com).

After connecting, I need to select the Azure subscription my Virtual machine is located in if you have only one subscription you can skip this part.

The command below will show all Azure subscriptions.

get-AzSubscription

To select an Azure subscription, I will run the command below with the name of the Azure subscription.

Select-AzSubscription –SubscriptionName "pay-as-you-go"

Now, that I am connected to the correct subscription, I will turn off the VM using the command below.

stop-vm -resoursegroup VMname -name vmname

Azure Variables

In the two variables below, I will enter the name of the resource group the VM is in and the name of the Virtual Machine I’m going to add storage to and run the two lines.

$Resource_Group = 'Resource_Group_Name'
$Virtual_Machine_Name = 'VM Name'

The next command will create a variable with the virtual machine name.

$azurevm = Get-AzVM -ResourceGroupName $Resource_Group -Name $Virtual_Machine_Name

Now, I will run a command that will keep the virtual machine disk details in a variable called “disk”.

$disk= Get-AzDisk -ResourceGroupName $Resource_Group -DiskName $azurevm.StorageProfile.OsDisk.Name

The command below will set the size of the new disk, In my case the current disk size is 127GB and I’m going to increase it to 500GB.

Just remember that the disk size cannot be decreased.

$disk.DiskSizeGB = 500

The final command will increase the disk size using all the variables above.

Update-AzDisk -ResourceGroupName $ResourceGroupName $Resource_Group -Disk $disk -DiskName $disk.Name

As you can see below, the new disk size is set to 500GB, and all I need to do is use the Windows disk manager utility to expand the disk size.


Posted

in

by