Azure Automation: How to add a data disk to an Azure VM w/PowerShell


This article is about two new functions from my Azure Automation Az-ModuleGet-AzVmDisk and New-AzVmDisk.

The built-in Get-AzureRmVm cmdlet returns a VM object in the JSON format. The Get-AzVmDisk function gets this output and makes with it three actions:

  • Cuts disks related data

  • Parses this data

  • Returns PowerShell like structured objects


  • For multiple VM

Get-AzureRmVM -ResourceGroupName $AzResourceGroup | Get-AzVmDisk
Get-AzureRmVM -ResourceGroupName $AzResourceGroup | Get-AzVmDisk –DiskType OSDisk
Get-AzureRmVM -ResourceGroupName $AzResourceGroup | Get-AzVmDisk DataDisk | Format-Table -au
  • Interactively for a single VM
Select-AzResourceGroup | Select-AzObject VM | Get-AzVmDisk
Select-AzObject VM | Get-AzVmDisk
  • The Get-AzVmDisk function returns [AzDisk] objects with the following properties.
Select-AzResourceGroup | Select-AzObject VM | Get-AzVmDisk | Get-Member -MemberType Property
Property Description
ResourceGroup VM ResourceGroup name
VM Virtual Machine name
VMSize VM size (restricts maximum data disk number)
DiskName Logical disk name (disk object name)
DiskType Disk type (OSDisk/DataDisk)
Index DataDisks Array Index, -1 for OSDisk
Lun Data disk LUN (Logical Unit Number), -1 for OSDisk
StorageAccount Disk’s parent StorageAccount name
Container Blob container (aka parent folder name)
Vhd Physical disk file name (*.vhd file)
Path Full disk file path
SizeGB Disk file size (GiB)
Cache Disk caching mode (None/ReadOnly/ReadWrite)
Created Disk creation method (Attach/FromImage/Empty)
  • You can select only properties you need with -ExcludeProperty parameter of Select-Object cmdlet. If you are using my New-AzCredProfile function, you have the $AzResourceGroup variable already set.
Get-AzureRmVM -wa SilentlyContinue -ResourceGroupName $AzResourceGroup | Get-AzVmDisk | select * -exclude Container, Path | Format-Table -au



  • The New-AzVmDisk function calls the Get-AzVmDisk function twice, before and after adding a new disk.


  • For OSDisk disk type the Lun always will be equal to -1 (this property is not relevant for system disks) and disk size SizeGB will be 0 (zero). The Microsoft does not supply this information Emoj. At least I have not found it…

Disk caching mode

  • If you are not sure what caching mode to choice -Caching parameter (None/ReadOnly/ReadWrite) for your disk, you have an alternative method to choice the disk purpose (SqlData/SqlTempDB/SqlLog) and the function will pick the best practice choice for you. The -Caching parameter supports Intellisense (Ctrl+Space or TAB completion).

  • While a disk has been added you will see the progress with some info about the new disk.

VMSize restrictions

  • Here we tried to add third data disk, but VMSize restriction did not allow doing that.


  • Have you forgotten about Get-AzVmDisk? One of the returned properties for each disk is the parent VM’s VMSize.


  • The built-in Get-AzureRmVMSize cmdlet will help us to know the VMSize disk related restrictions (MaxDataDiskCount property). The VMSizes may differ depending on Location.
Get-AzureRmVMSize -Location ((Get-AzureRmResourceGroup –Name $AzResourceGroup).Location) | select Name, max* | sort Name | ft –au
  • You can group the VMSize by MaxDataDiskCount property. This code seems ugly, but his output will give you all VMSizes that meet your VM disk requirements.
Get-AzureRmVMSize -Location ((Get-AzureRmResourceGroup -Name $AzResourceGroup).Location) | select Name, MaxDataDiskCount | sort MaxDataDiskCount | group MaxDataDiskCount | select @{ N = 'MaxDataDiskCount'; E = { $_.Name } }, Count, @{ N = 'VMSize'; E = { ($_ | select -expand Group | sort Name).Name -join ', ' } } | ft -au –Wrap


–StorageAccount parameter

  • The New-AzVmDisk supports –StorageAccount parameter. This parameter can be one of the three options (OSDisk, FirstDataDisk or Prompt). The Intellisense (Ctrl+Space or TAB completion) is supported.

–StorageAccount Prompt

  • Option Prompt will show you the list of all StorageAccounts that are in your VM’s ResourceGroup. This menu generated by Write-Menu function, included in the module.

–StorageAccount FirstDataDisk

  • Option FirstDataDisk will place your newly created data disk on the same StorageAccount with very first data disk already attached to your VM. If no one data disk exists, the new disk will be placed on the system disk’s StorageAccount.

–StorageAccount OSDisk

  • The last option OSDisk is self-explained.

Container property

  • Note regarding the Container property, returned by Get-AzVmDisk. No matter what is its name in your environment, but the New-AzVmDisk assumes it exists on all StorageAccounts where you are planning to deploy new data disks!

  • The built-in Get-AzureStorageContainer cmdlet will help you to discover all your StorageAccounts.

Get-AzureRmStorageAccount | Get-AzureStorageContainer | select @{N='StorageAccount';E={$_.Context.StorageAccountName}}, @{N='Container';E={$_.Name}} | ? {$_.Container -notmatch '^bootdiag'} | sort StorageAccountName, Container
  • The following one-liner will find all your StorageAccounts that contain Container, named vhds.
Get-AzureRmStorageAccount | Get-AzureStorageContainer | select @{N='StorageAccount';E={$_.Context.StorageAccountName}}, @{N='Container';E={$_.Name}} | ? {$_.Container -eq 'vhds'} | sort StorageAccountName



  • Both functions can operate with multiple VM. In the case of New-AzVmDisk, a single data disk with the same settings (caching mode and disk size) will be added to all passed VMs. What may vary from VM to VM is a disk placement, controlled by -StorageAccount parameter.
Get-AzureRmVM -ResourceGroupName $AzResourceGroup | New-AzVmDisk –StorageAccount FirstDataDisk
Get-AzureRmVM -ResourceGroupName $AzResourceGroup | New-AzVmDisk -SizeGB 100 -Caching ReadWrite –StorageAccount Prompt
  • For more details about the functions, please take a look at the content based help and examples.
Get-Alias -Definition New-AzVmDisk
Get-Help New-AzVmDisk -Full
Get-Help Get-AzVmDisk -Examples
Get-Help New-AzVmDisk -Parameter SizeGB

You may also like:

How to find orphaned VHD files in the Azure
How to login to the Azure automatically
Azure VM Tag automation
Azure Automation PowerShell Az-Module Home

3 thoughts on “Azure Automation: How to add a data disk to an Azure VM w/PowerShell

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s