{"id":3552,"date":"2013-05-29T00:01:00","date_gmt":"2013-05-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/05\/29\/use-powershell-to-initialize-raw-disks-and-to-partition-and-format-volumes\/"},"modified":"2013-05-29T00:01:00","modified_gmt":"2013-05-29T00:01:00","slug":"use-powershell-to-initialize-raw-disks-and-to-partition-and-format-volumes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-initialize-raw-disks-and-to-partition-and-format-volumes\/","title":{"rendered":"Use PowerShell to Initialize Raw Disks and to Partition and Format Volumes"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to initialize raw disks and to partition and format volumes.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. In yesterday&rsquo;s post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/05\/28\/use-powershell-to-add-vhds-and-configure-new-virtual-machines.aspx\" target=\"_blank\">Use PowerShell to Add VHDs and Configure New Virtual Machines<\/a>, I was able to create and add new VHDs to previously existing virtual machines. However, when the virtual machine comes online, there is a bit of work to do to make the drive accessible from within the operating system. For example, the drive needs to be initialized. This occurs, typically when I open the Disk Management utility and a message states that a new drive is detected and must be initialized. This is typically a one-time operation, and it does not take very long to accomplish. Next, the drive must be partitioned and formatted before it is usable. Formatting a new drive can take a bit of time depending on the size of the drive and the type of format being performed.\nLuckily, with Windows PowerShell 3.0 in Windows 8 or Windows Server&nbsp;2012, I can perform all of these operations via Windows PowerShell functions from the Storage module. The process is the same as I would do via the Disk Management tool. The steps are:<\/p>\n<ol>\n<li>Get the disk that has a <em>raw <\/em>partition style.<\/li>\n<li>Initialize the disk.<\/li>\n<li>Partition the disk.<\/li>\n<li>Format the volume.<\/li>\n<\/ol>\n<p>The following script accomplishes these four tasks:<\/p>\n<p style=\"padding-left: 30px\"><strong>InitializePartitionFormatNewDisks.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\">Get-Disk |<\/p>\n<p style=\"padding-left: 30px\">Where partitionstyle -eq &#8216;raw&#8217; |<\/p>\n<p style=\"padding-left: 30px\">Initialize-Disk -PartitionStyle MBR -PassThru |<\/p>\n<p style=\"padding-left: 30px\">New-Partition -AssignDriveLetter -UseMaximumSize |<\/p>\n<p style=\"padding-left: 30px\">Format-Volume -FileSystem NTFS -NewFileSystemLabel &#8220;disk2&#8221; -Confirm:$false\n&nbsp;<span style=\"font-size: 12px\">The first thing I do is get all disks that have a <\/span><em style=\"font-size: 12px\">raw <\/em><span style=\"font-size: 12px\">partition style. To do this I use the <\/span><strong style=\"font-size: 12px\">Get-Disk<\/strong><span style=\"font-size: 12px\"> function, and I use the <\/span><strong style=\"font-size: 12px\">Where-Object<\/strong><span style=\"font-size: 12px\"> cmdlet to limit the results to those with a <\/span><strong style=\"font-size: 12px\">&lsquo;Raw&rsquo;<\/strong><em style=\"font-size: 12px\"> <\/em><span style=\"font-size: 12px\">partition style. This portion of the script is shown here.<\/span><\/p>\n<p style=\"padding-left: 30px\">Get-Disk |<\/p>\n<p style=\"padding-left: 30px\">Where partitionstyle -eq &#8216;raw&#8217; |\nNow I use the <strong>Initialize-Disk<\/strong> function to initialize the raw disk. I specify that I want to use an MBR style of partition, and I use the <strong>PassThru<\/strong><em> <\/em>switch to pass the returned disk object down the pipeline to the next command. This portion of the script is shown here.<\/p>\n<p style=\"padding-left: 30px\">Initialize-Disk -PartitionStyle MBR -PassThru |\nBecause the disk is now initialized, I can create a new partition for the drive. To do this, I use <strong>the New-Partition<\/strong> function, and I allow the operating system to assign a new drive letter to the drive. I also tell the operating system to create the largest partition size that the disk will support. I then pipe the newly created partition to the next command. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">New-Partition -AssignDriveLetter -UseMaximumSize |\nThe last thing to do is to format the volume. To do this, I use the <strong>Format-Volume<\/strong> function, and I specify that I want to format the volume by using NTFS. Because this is my second disk for each of my virtual machines, I specify the disk label as <strong>&ldquo;disk 2&rdquo;<\/strong>, and I suppress the confirmation prompt. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Format-Volume -FileSystem NTFS -NewFileSystemLabel &#8220;disk2&#8221; -Confirm:$false\nWhen I run the script, the script initializes the raw disk, partitions the disk, and formats the newly created volume while adding my specified volume label. I am using the script on a bunch of virtual machines that I created, but the script also works on physical computers. This script is one that I copied to the system volume when I mounted the volumes and copied files to them prior to bringing the virtual machines online.\nThat is all there is to using Windows PowerShell to initialize raw disks and to partition and format a new volume. Join me tomorrow when I will talk about Event 6 in the 2013 Scripting Games.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to initialize raw disks and to partition and format volumes. Microsoft Scripting Guy, Ed Wilson, is here. In yesterday&rsquo;s post, Use PowerShell to Add VHDs and Configure New Virtual Machines, I was able to create and add new VHDs to previously existing virtual machines. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[216,3,12,45],"class_list":["post-3552","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-disk-drives-and-volumes","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to initialize raw disks and to partition and format volumes. Microsoft Scripting Guy, Ed Wilson, is here. In yesterday&rsquo;s post, Use PowerShell to Add VHDs and Configure New Virtual Machines, I was able to create and add new VHDs to previously existing virtual machines. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3552"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3552\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}