Azure VM | Add Multiple Data Disks v2

This is a new version of my previous script Add multiple managed disks to Azure RM VM


    • Shows the disk capacity of the VM and asks how many disks to add.
    • Prompts for the size of every disk
    • Checks for empty Luns to add the disks
# 1. You need to login to the Azure Rm Account


# 2. The script will query the Subscriptions that the login account has access and will promt the user to select the target Subscription from the drop down list
$subscription = Get-AzSubscription | Out-GridView -Title "Select a Subscription" -PassThru
Select-AzSubscription -SubscriptionId $subscription.Id

# 3. The script will query the available VMs and promt to select the target VM from the VM list
$vm = Get-AzVM | Out-GridView -Title "Select the Virtual Machine to add Data Disks to" -PassThru

# 4. I set the storage type based on the OS disk. If you want to spesify somehting else you can cahnge this to: $storageType = StandardLRS or PremiumLRS etc.

$storageType = $VM.StorageProfile.OsDisk.ManagedDisk.StorageAccountType

# 5. Enter how many data disks you need to create

$VMdiskCapacity = ($VM.StorageProfile.DataDisks).Capacity
$diskquantity = Read-Host "How many disks you need to create? Max Capacity" $VMdiskCapacity "."

# 6. The script will promt for disk size, in GB

$diskSizeList = @()
for($i = 1; $i -le $diskquantity; $i++)
    $disk = (Read-Host "Disk " $i " Size")
    $diskSizeList += $disk

# 7. check for exisiting disks

$existinglun = @()
for($i = 0; $i -lt $VMdiskCapacity; $i++) {
    $existinglun += ($VM.StorageProfile.DataDisks)[$i].Lun

# 8. disks creation

for($i = 0; $i -lt $diskquantity; $i++)
$diskName = $vm.Name + "-DataDisk-" + $i.ToString()
$diskConfig = New-AzDiskConfig -AccountType $storageType -Location $vm.Location -CreateOption Empty -DiskSizeGB $diskSizeList[$i]
$DataDisk = New-AzDisk -DiskName $diskName -Disk $diskConfig -ResourceGroupName $vm.ResourceGroupName
for ($j = 0; $j -lt $VMdiskCapacity; $j++) {
    if ( $null -eq $existinglun[$j] ) {
        for ($k = 0; $k -lt $VMdiskCapacity; $k++ ) {
            $nextLunIndex = $k
            for ( $m = 0; $m -lt $VMdiskCapacity; $m++ ) {
                if ( $k -eq $existinglun[$m] ) {
                    $nextLunIndex = -1 
            if ($nextLunIndex -ne -1 ) {
        Add-AzVMDataDisk -VM $vm -Name $DiskName -CreateOption Attach -ManagedDiskId $DataDisk.Id -Lun $nextLunIndex
        $existinglun[$j] = $nextLunIndex
Update-AzVM -VM $vm -ResourceGroupName $vm.ResourceGroupName

You can download the script from:



  1. Excelent, Pantelis! Thank you so much for this article.

    Do you have, by any chance, a script for a scenario in which the disks already exist?

    I need to create a routine to attach existing managed data disks to a VM, but I couldn’t quite figure it out how to do it yet.

    Thanks in advance!

  2. Hi Leonardo,

    you can attach exising this, using a csv list with the disk name and lun, like:

    and use a script like:
    $rg = “ResourceGroupName”
    $vmname = “VirtualMachineName”
    $vm = Get-AzVM -ResourceGroupName $rg -Name $vmname
    $disklist = Import-Csv ./diskname.csv
    foreach ($disk in $disklist) {
    $datadisk = Get-AzDisk -ResourceGroupName $rg -DiskName $disk.diskname
    $vm = Add-AzVMDataDisk -VM $vm -CreateOption Attach -ManagedDiskId $datadisk.Id -Lun $disk.lun
    Update-AzVM -VM $vm -ResourceGroupName $rg

    of course I have not tested this 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.