Creating and Attaching Disks in Azure

Data disks are recommended when durable and responsive data storage is required. When installing applications and storing data on Azure virtual machines, data disks should be used. This tutorial walks you through the process of creating an Azure data disk and connecting it to an Azure virtual machine - all via PowerShell.

Creating a disk, attaching it to a virtual machine, and preparing it for use requires five steps.  The first step is to create the initial disk configuration.  Once the configuration is created, the actual data disk resource is created.  After creating the configuration and the actual data disk, the data disk is added to the virtual machine configuration.  Next, the virtual machine configuration is updated to include the new disk. Lastly, the disk is initialized and formatted for use.

EDITOR'S NOTE:  This tutorial was excerpted from the complete online video course, "Creating and Managing Azure Virtual Machines with PowerShell", which can be found on the Udemy training platform.

The exercises below demonstrate how to create a new data disk and how to add the data disk to a virtual machine. Follow the instructions below to use PowerShell to create a new data disk and then to attach the data disk to a virtual machine called myVM. Of course, before getting started, you will need to deploy a virtual machine called myVM into a resource group called "VMLab".

Click here if you need help with connecting to Azure via PowerShell and with deploying an Azure virtual machine.

STEP 1: Create Data Disk Configuration

Create the initial disk configuration with the New-AzureRmDiskConfig command. The command below configures a disk that is 128 gigabytes in size. Copy and paste it into your PowerShell session and run it.  The new disk configuration is stored in a variable called $diskConfig so it can be more easily referenced in later commands.

$diskConfig = New-AzureRmDiskConfig -Location "EastUS" -CreateOption Empty -DiskSizeGB 128

The command above generates no feedback on the screen.

STEP 2: Create the Data Disk

With the data disk configuration created, create an actual data disk, called "DataDisk", with the New-AzureRmDisk command below:

$dataDisk = New-AzureRmDisk -ResourceGroupName "VMLab" -DiskName "DataDisk" -Disk $diskConfig

The command above takes a few minutes to complete and produces no feedback onscreen.  What New-AzureRmDisk does is create an actual data disk resource that’s based on the disk configuration that you created in the first step (notice how $diskConfig is specified in the -Disk switch).

STEP 3: Add Data Disk to Virtual Machine

With the data disk created, retrieve the virtual machine config that you are adding the data disk to, by running with the Get-AzureRmVM command below.

$vm = Get-AzureRmVM -ResourceGroupName "VMLab" -Name "myVM"

The command above retrieves the virtual machine configuration for the MyVM virtual machine and loads it into a variable called $vm.  You can add the data disk to the virtual machine configuration with the Add-AzureRmVMDataDisk command below.

$vm = Add-AzureRmVMDataDisk -VM $vm -Name "DataDisk" -CreateOption Attach -ManagedDiskId $dataDisk.Id -Lun 1

The command above doesn’t actually update the virtual machine yet.  What it does is add the new data disk to the virtual machine configuration.  The next step will take that updated configuration and apply it to the actual virtual machine.

STEP 4: Update the Virtual Machine

With the data disk configuration added to the virtual machine configuration (using the command above), the virtual machine can be updated with the Update-AzureRmVM command below:

Update-AzureRmVm -ResourceGroupName "VMLab" -VM $vm

The command above adds the actual data disk to the virtual machine so that the OS of the virtual machine can see it.  It can take a minute or two to complete.

STEP 5: Manually Configure the Attached Disk

After attaching a data disk to a virtual machine, the disk must be configured within the operating system on the virtual machine itself.  The disk must be initialized, and a partition created.  The partition must then be formatted (just like any other physical disk would be). The following exercise demonstrates how to manually configure a data disk added to a virtual machine.

To manually configure the newly-attached data disk on myVM, simply create an RDP connection to the virtual machine and open PowerShell on it.

Run the command below in the PowerShell session on the myVM server and hit Enter:

Get-Disk | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "DataDisk" -Confirm:$false

The command above is one long command.  It retrieves the raw disk, using the Get-Disk command. From there, it pipes that information to the Initialize-Disk command so that the disk can be initialized within the operating system. Once the disk is initialized, the New-Partition command creates a new partition that equals the size of the disk.  Lastly, the Format-Volume command formats the new volume so that it can be accessed by the operating system.

If you've done everything correctly, you should now see a 128GB drive on the myVM virtual machine.

More Learning Resources

If you’d like to learn more about topics like this one, visit me at my website or at my training site.  You can also find me on LinkedIn and on Facebook.  If you are after free video tutorials, visit my YouTube channel.  Some of my courses, like this one, can also be found on, where you can use coupon code THOMASMITCHELL at checkout to get any one of my complete online courses for just $12.99.

Click here to join the Understanding Azure Facebook group or here for the latest Azure practice questions, answers, explanations, and reference materials.

Thomas Mitchell

Tom is a 20+ year veteran of the IT industry and carries numerous Microsoft certifications, including the MCSE: Cloud Platform and Infrastructure certification. A Subject Matter Expert in Active Directory and Microsoft Exchange, Tom also possesses expert-level knowledge in several other IT disciplines, including Azure, Storage, and O365/Exchange Online. You can find Tom at his website, on LinkedIn, or on Facebook. Need to reach him by phone? Call 484-334-2790.