Attach a data disk to a Windows VM with PowerShell
On this page, you’ll understand data disk and how to attach both new and existing data disks to a Windows VM by using PowerShell.
So, to do so:
- The size of the virtual machine controls how many data disks you can attach. For more information, see Sizes for virtual machines.
- To use premium SSDs, you’ll need a premium storage-enabled VM type, like the DS-series or GS-series virtual machine.
This article uses PowerShell within the Azure Cloud Shell, which is constantly updated to the latest version. To open the Cloud Shell, select Try it from the top of any code block.
Add an empty data disk to a virtual machine
This example shows how to add an empty data disk to an existing virtual machine.
Using managed disks
$rgName = 'myResourceGroup' $vmName = 'myVM' $location = 'East US' $storageType = 'Premium_LRS' $dataDiskName = $vmName + '_datadisk1' $diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB 128 $dataDisk1 = New-AzDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $rgName $vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName $vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 1 Update-AzVM -VM $vm -ResourceGroupName $rgName
Using managed disks in an Availability Zone
To create a disk in an Availability Zone, use New-AzDiskConfig with the -Zone parameter. The following example creates a disk in zone 1.
$rgName = 'myResourceGroup' $vmName = 'myVM' $location = 'East US 2' $storageType = 'Premium_LRS' $dataDiskName = $vmName + '_datadisk1' $diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB 128 -Zone 1 $dataDisk1 = New-AzDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $rgName $vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName $vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 1 Update-AzVM -VM $vm -ResourceGroupName $rgName
Initialize the disk
After you add an empty disk, you’ll need to initialize it. To initialize the disk, you can sign in to a VM and use disk management. If you enabled WinRM and a certificate on the VM when you created it, you can use remote PowerShell to initialize the disk. You can also use a custom script extension:
$location = "location-name" $scriptName = "script-name" $fileName = "script-file-name" Set-AzVMCustomScriptExtension -ResourceGroupName $rgName -Location $locName -VMName $vmName -Name $scriptName -TypeHandlerVersion "1.4" -StorageAccountName "mystore1" -StorageAccountKey "primary-key" -FileName $fileName -ContainerName "scripts"
Attach an existing data disk to a VM
You can attach an existing managed disk to a VM as a data disk.
$rgName = "myResourceGroup" $vmName = "myVM" $location = "East US" $dataDiskName = "myDisk" $disk = Get-AzDisk -ResourceGroupName $rgName -DiskName $dataDiskName $vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName $vm = Add-AzVMDataDisk -CreateOption Attach -Lun 0 -VM $vm -ManagedDiskId $disk.Id Update-AzVM -VM $vm -ResourceGroupName $rgName
Reference: Microsoft Documentation