# Create Multiple Disks on VM

**URL:** <https://forums.powershell.org/t/create-multiple-disks-on-vm/12946>\
**Category:** PowerShell Help\
**Created:** [July 31, 2019, 7:19am UTC](https://forums.powershell.org/t/create-multiple-disks-on-vm/12946 "2019-07-31T07:19:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anelliaf](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anelliaf](https://forums.powershell.org/u/anelliaf)\
**Post date:** [July 31, 2019, 7:19am UTC](https://forums.powershell.org/t/create-multiple-disks-on-vm/12946/1 "2019-07-31T07:19:35Z")

</div>

Hello,

I have a basic script where I can create one disk on a VM. I’d like to be able to create multiple disk with different sizes and names. I’m unsure how to get started on this.

This is what I have so far:

[pre]

## connect to Azure

Connect-AzAccount

Set-AzContext -Subscription “\<sub\_name\>”

$rgName = “\<rg\_name\>”  
$vmName = “azr-tst-01”  
$diskName = $vmName.Substring(0,12) + “\_Data”

# create the initial configuration

$diskConfig = New-AzDiskConfig `  
-Location “EastUS2” `  
-CreateOption Empty `  
-DiskSizeGB 100

# create the data disk

$dataDisk = New-AzDisk `  
-ResourceGroupName $rgName `  
-DiskName $diskName `  
-Disk $diskConfig

# get the VM where you want to add the data disk

$vm = Get-AzVM -ResourceGroupName $rgName `  
-Name $vmName

# add the data disk to the VM configuration

$vm = Add-AzVMDataDisk `  
-VM $vm `  
-Name $diskName `  
-CreateOption Attach `  
-ManagedDiskId $dataDisk.Id `  
-Lun 1

# update the VM

Update-AzVM -ResourceGroupName $rgName -VM $vm

# prepare the data disks

#Get-AzDisk -DiskName $diskName | Where partitionstyle -EQ ‘raw’

[/pre]

&nbsp;

Thanks,

Frank

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [July 31, 2019, 7:24am UTC](https://forums.powershell.org/t/create-multiple-disks-on-vm/12946/2 "2019-07-31T07:24:16Z")

</div>

You repeat lines 9-36 for each disk

---

<div class="post-metadata">

**Author:** ![anelliaf](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anelliaf](https://forums.powershell.org/u/anelliaf)\
**Post date:** [July 31, 2019, 1:15pm UTC](https://forums.powershell.org/t/create-multiple-disks-on-vm/12946/3 "2019-07-31T13:15:29Z")

</div>

A for loop as such (just an example) before 9 and after 36:

$disks = 4  
$vmName = “azr-tst-01”  
$diskName = $vmName.Substring(0,12) + “\_Data”

for ($i=1; $i -le $disks; $i++) {  
Write-Host $diskName $i  
}

But how do I have a unique name for each disk? I’d like to add a special name like data, log, etc for each disk, not just a number.

&nbsp;

---

<div class="post-metadata">

**Author:** ![panzerbjrn](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/panzerbjrn/32/36_2.png) [@panzerbjrn](https://forums.powershell.org/u/panzerbjrn)\
**Post date:** [August 1, 2019, 5:02am UTC](https://forums.powershell.org/t/create-multiple-disks-on-vm/12946/4 "2019-08-01T05:02:40Z")

</div>

Then you could create a loop for each of those names, or in the loop, have disk 1 be Data, 2 be logs and so on.  
Example: IF ($I -eq 1) {$Diskname = Data}

---

<div class="post-metadata">

**Author:** ![anelliaf](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anelliaf](https://forums.powershell.org/u/anelliaf)\
**Post date:** [August 1, 2019, 10:18am UTC](https://forums.powershell.org/t/create-multiple-disks-on-vm/12946/5 "2019-08-01T10:18:02Z")

</div>

Thanks for that!

i.e.)

[pre]

$disks = 4  
$vmName = “azr-tst-01”  
$diskName = $vmName.Substring(0,12) + ‘\_00’

for ($i=1; $i -le $disks; $i++) {

if ($i -eq 1) {  
Write-Host ($diskName + $i)  
}  
elseif ($i -eq 2) {  
Write-Host ($diskname + $i)  
}  
else {  
Write-Host “No more disks left”  
}  
}

[/pre]

How would you incorporate varying disk sizes?

---

<div class="post-metadata">

**Author:** ![anelliaf](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anelliaf](https://forums.powershell.org/u/anelliaf)\
**Post date:** [August 1, 2019, 12:16pm UTC](https://forums.powershell.org/t/create-multiple-disks-on-vm/12946/6 "2019-08-01T12:16:39Z")

</div>

This would be the loop, but I’m unsure how I incorporate the if statement:?

[pre]

for ($i=1; $i -le $numDisks; $i++) {

# create the initial configuration

$diskConfig = New-AzDiskConfig `  
-Location “EastUS2” `  
-CreateOption Empty `  
-DiskSizeGB 100

# create the data disk

$dataDisk = New-AzDisk `  
-ResourceGroupName $rgName `  
-DiskName $diskName `  
-Disk $diskConfig

# get the VM where you want to add the data disk

$vm = Get-AzVM -ResourceGroupName $rgName `  
-Name $vmName

# add the data disk to the VM configuration

$vm = Add-AzVMDataDisk `  
-VM $vm `  
-Name $diskName `  
-CreateOption Attach `  
-ManagedDiskId $dataDisk.Id `  
-Lun 1  
}

[/pre]

---

<div class="post-metadata">

**Author:** ![an0nemus](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@an0nemus](https://forums.powershell.org/u/an0nemus)\
**Post date:** [August 2, 2019, 10:49am UTC](https://forums.powershell.org/t/create-multiple-disks-on-vm/12946/7 "2019-08-02T10:49:02Z")

</div>

It would probably be easier to create a csv file, and pull that as variables into your script.

```
$disks=(import-csv Disks.csv)
foreach ($disk in $disks){
your script logic
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/create-multiple-disks-on-vm/12946/8 "2024-05-16T20:32:06Z")

</div>


