{"id":18314,"date":"2020-08-27T14:19:43","date_gmt":"2020-08-27T14:19:43","guid":{"rendered":"https:\/\/www.testpreptraining.com\/tutorial\/?page_id=18314"},"modified":"2020-08-27T14:19:45","modified_gmt":"2020-08-27T14:19:45","slug":"attach-a-data-disk-to-a-windows-vm-with-powershell","status":"publish","type":"page","link":"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/","title":{"rendered":"Attach a data disk to a Windows VM with PowerShell"},"content":{"rendered":"\n<p class=\"has-text-align-right\"><strong><a href=\"https:\/\/www.testpreptraining.ai\/tutorial\/exam-az-104-microsoft-azure-administrator-associate\/\" target=\"_blank\" rel=\"noreferrer noopener\">Return to AZ-104 Tutorial<\/a><\/strong><\/p>\n\n\n\n<p>On this page, you\u2019ll understand data disk and how to attach both new and existing data disks to a Windows VM by using PowerShell.<\/p>\n\n\n\n<p>So, to do so:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The size of the virtual machine controls how many data disks you can attach. For more information, see Sizes for virtual machines.<\/li><li>To use premium SSDs, you&#8217;ll need a premium storage-enabled VM type, like the DS-series or GS-series virtual machine.<\/li><\/ul>\n\n\n\n<p>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.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Add an empty data disk to a virtual machine<\/strong><\/h4>\n\n\n\n<p>This example shows how to add an empty data disk to an existing virtual machine.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using managed disks<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">$rgName = 'myResourceGroup'\n$vmName = 'myVM'\n$location = 'East US'\n$storageType = 'Premium_LRS'\n$dataDiskName = $vmName + '_datadisk1' \n$diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB 128\n$dataDisk1 = New-AzDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $rgName\n$vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName\n$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 1\nUpdate-AzVM -VM $vm -ResourceGroupName $rgName<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using managed disks in an Availability Zone<\/strong><\/h4>\n\n\n\n<p>To create a disk in an Availability Zone, use New-AzDiskConfig with the -Zone parameter. The following example creates a disk in zone 1.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$rgName = 'myResourceGroup'\n$vmName = 'myVM'\n$location = 'East US 2'\n$storageType = 'Premium_LRS'\n$dataDiskName = $vmName + '_datadisk1'\n$diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB 128 -Zone 1\n$dataDisk1 = New-AzDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $rgName\n$vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName\n$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 1\nUpdate-AzVM -VM $vm -ResourceGroupName $rgName<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Initialize the disk<\/strong><\/h4>\n\n\n\n<p>After you add an empty disk, you&#8217;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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$location = \"location-name\"\n$scriptName = \"script-name\"\n$fileName = \"script-file-name\"\nSet-AzVMCustomScriptExtension -ResourceGroupName $rgName -Location $locName -VMName $vmName -Name $scriptName -TypeHandlerVersion \"1.4\" -StorageAccountName \"mystore1\" -StorageAccountKey \"primary-key\" -FileName $fileName -ContainerName \"scripts\"<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Attach an existing data disk to a VM<\/strong><\/h4>\n\n\n\n<p>You can attach an existing managed disk to a VM as a data disk.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$rgName = \"myResourceGroup\"\n$vmName = \"myVM\"\n$location = \"East US\"\n$dataDiskName = \"myDisk\"\n$disk = Get-AzDisk -ResourceGroupName $rgName -DiskName $dataDiskName\n$vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName\n$vm = Add-AzVMDataDisk -CreateOption Attach -Lun 0 -VM $vm -ManagedDiskId $disk.Id\nUpdate-AzVM -VM $vm -ResourceGroupName $rgName<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/www.testpreptraining.ai\/microsoft-azure-administrator-associate-az-104-online-course\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/www.testpreptraining.ai\/tutorial\/wp-content\/uploads\/2020\/07\/testpreptraining.ai-2-750x117.png\" alt=\"data disk\"\/><\/a><figcaption>data disk<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"has-text-align-right\"><strong>Reference: <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/virtual-machines\/windows\/attach-disk-ps\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft Documentation<\/a><\/strong><\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.testpreptraining.ai\/tutorial\/exam-az-104-microsoft-azure-administrator-associate\/\" target=\"_blank\" rel=\"noreferrer noopener\">Return to AZ-104 Tutorial<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Return to AZ-104 Tutorial On this page, you\u2019ll 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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-18314","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Attach a data disk to a Windows VM with PowerShell - Testprep Training Tutorials<\/title>\n<meta name=\"description\" content=\"Increase your skills by learning about data disk with configuring VMs using Microsoft Azure AZ-104 online course Now! Try now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Attach a data disk to a Windows VM with PowerShell - Testprep Training Tutorials\" \/>\n<meta property=\"og:description\" content=\"Increase your skills by learning about data disk with configuring VMs using Microsoft Azure AZ-104 online course Now! Try now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"Testprep Training Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-27T14:19:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.testpreptraining.ai\/tutorial\/wp-content\/uploads\/2020\/07\/testpreptraining.ai-2-750x117.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/\",\"url\":\"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/\",\"name\":\"Attach a data disk to a Windows VM with PowerShell - Testprep Training Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#website\"},\"datePublished\":\"2020-08-27T14:19:43+00:00\",\"dateModified\":\"2020-08-27T14:19:45+00:00\",\"description\":\"Increase your skills by learning about data disk with configuring VMs using Microsoft Azure AZ-104 online course Now! Try now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.testpreptraining.ai\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Attach a data disk to a Windows VM with PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#website\",\"url\":\"https:\/\/www.testpreptraining.ai\/tutorial\/\",\"name\":\"Testprep Training Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.testpreptraining.ai\/tutorial\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#organization\",\"name\":\"Testprep Training\",\"url\":\"https:\/\/www.testpreptraining.ai\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.testpreptraining.com\/tutorial\/wp-content\/uploads\/2020\/07\/tpt-logo-6.png\",\"contentUrl\":\"https:\/\/www.testpreptraining.com\/tutorial\/wp-content\/uploads\/2020\/07\/tpt-logo-6.png\",\"width\":583,\"height\":153,\"caption\":\"Testprep Training\"},\"image\":{\"@id\":\"https:\/\/www.testpreptraining.ai\/tutorial\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Attach a data disk to a Windows VM with PowerShell - Testprep Training Tutorials","description":"Increase your skills by learning about data disk with configuring VMs using Microsoft Azure AZ-104 online course Now! Try now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/","og_locale":"en_US","og_type":"article","og_title":"Attach a data disk to a Windows VM with PowerShell - Testprep Training Tutorials","og_description":"Increase your skills by learning about data disk with configuring VMs using Microsoft Azure AZ-104 online course Now! Try now!","og_url":"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/","og_site_name":"Testprep Training Tutorials","article_modified_time":"2020-08-27T14:19:45+00:00","og_image":[{"url":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-content\/uploads\/2020\/07\/testpreptraining.ai-2-750x117.png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/","url":"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/","name":"Attach a data disk to a Windows VM with PowerShell - Testprep Training Tutorials","isPartOf":{"@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#website"},"datePublished":"2020-08-27T14:19:43+00:00","dateModified":"2020-08-27T14:19:45+00:00","description":"Increase your skills by learning about data disk with configuring VMs using Microsoft Azure AZ-104 online course Now! Try now!","breadcrumb":{"@id":"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/attach-a-data-disk-to-a-windows-vm-with-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.testpreptraining.ai\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Attach a data disk to a Windows VM with PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#website","url":"https:\/\/www.testpreptraining.ai\/tutorial\/","name":"Testprep Training Tutorials","description":"","publisher":{"@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.testpreptraining.ai\/tutorial\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#organization","name":"Testprep Training","url":"https:\/\/www.testpreptraining.ai\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.testpreptraining.com\/tutorial\/wp-content\/uploads\/2020\/07\/tpt-logo-6.png","contentUrl":"https:\/\/www.testpreptraining.com\/tutorial\/wp-content\/uploads\/2020\/07\/tpt-logo-6.png","width":583,"height":153,"caption":"Testprep Training"},"image":{"@id":"https:\/\/www.testpreptraining.ai\/tutorial\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/pages\/18314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/comments?post=18314"}],"version-history":[{"count":2,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/pages\/18314\/revisions"}],"predecessor-version":[{"id":18316,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/pages\/18314\/revisions\/18316"}],"wp:attachment":[{"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/media?parent=18314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/categories?post=18314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testpreptraining.ai\/tutorial\/wp-json\/wp\/v2\/tags?post=18314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}