In this post i am going to walk you through the steps to expand or resize your OS Managed disk in your azure VMs. By default the OS disk in a Azure system is 127GB which is generally large enough for typical scenarios especially if you are following best practices of adding a Data drive and installing applications to it instead of the OS drive. Other scenarios would be when migrating on prem servers to azure a resize might be a good idea to correct or standardize configurations. Luckily the Azure Powershell Module has a really nice cmdlet to take care of this and make this extremely easy. Below are both of the steps needed to increase the disk size in Azure as well as resizing the partition within windows itself.
The First step is to install the Azure Powershell Module
Install-Module -Name Az –AllowClobber
Next we need to make a connection to azure and select your subscription
Select-AzSubscription –SubscriptionName ‘Your subscription name’
Now we can define our variables for your resource group VM Name and desired disk size.
$rgName = ‘RG’
$vmName = ‘servername’
$DiskSizeGB = ‘200’
$vm = Get-AzVM -ResourceGroupName $rgName -Name $vmName
In order to update the disk the VM must be stopped to increase the size so ensure to do this during planned downtime.
Stop-AzVM -ResourceGroupName $rgName -Name $vmName
After the VM is stopped we can perform the resize.
$disk= Get-AzDisk -ResourceGroupName $rgName -DiskName $vm.StorageProfile.OsDisk.Name
$disk.DiskSizeGB = $DiskSizeGB
Update-AzDisk -ResourceGroupName $rgName -Disk $disk -DiskName $disk.Name
Finally we can start the VM up again and the additional disk space will be available to extend the OS partition.
Start-AzVM -ResourceGroupName $rgName -Name $vmName
At this point you could go to disk management and extend your OS disk partition but whats the fun in that when we could easily automate that as well.
$size = (Get-PartitionSupportedSize -DiskNumber 0 -PartitionNumber 1)
Resize-Partition -DiskNumber 0 -PartitionNumber 1 -Size $size.SizeMax