Use PowerShell to Initialize Raw Disks and to Partition and Format Volumes

Use PowerShell to Initialize Raw Disks and to Partition and Format Volumes

  • Comments 8
  • Likes

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’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. 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.

Luckily, with Windows PowerShell 3.0 in Windows 8 or Windows Server 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:

  1. Get the disk that has a raw partition style.
  2. Initialize the disk.
  3. Partition the disk.
  4. Format the volume.

The following script accomplishes these four tasks:

InitializePartitionFormatNewDisks.ps1

Get-Disk |

Where partitionstyle -eq 'raw' |

Initialize-Disk -PartitionStyle MBR -PassThru |

New-Partition -AssignDriveLetter -UseMaximumSize |

Format-Volume -FileSystem NTFS -NewFileSystemLabel "disk2" -Confirm:$false

 The first thing I do is get all disks that have a raw partition style. To do this I use the Get-Disk function, and I use the Where-Object cmdlet to limit the results to those with a ‘Raw’ partition style. This portion of the script is shown here.

Get-Disk |

Where partitionstyle -eq 'raw' |

Now I use the Initialize-Disk function to initialize the raw disk. I specify that I want to use an MBR style of partition, and I use the PassThru switch to pass the returned disk object down the pipeline to the next command. This portion of the script is shown here.

Initialize-Disk -PartitionStyle MBR -PassThru |

Because the disk is now initialized, I can create a new partition for the drive. To do this, I use the New-Partition 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.

New-Partition -AssignDriveLetter -UseMaximumSize |

The last thing to do is to format the volume. To do this, I use the Format-Volume 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 “disk 2”, and I suppress the confirmation prompt. This command is shown here.

Format-Volume -FileSystem NTFS -NewFileSystemLabel "disk2" -Confirm:$false

When 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.

That 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.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • awesome, no mucking around with diskpart!

  • Get-Disk is not working on Windows 2008 R2 , Ive tried upgrading to Powershell 3.0 as well . still no joy . Does anyone have idea , do I need to add a specific snapin ?

  • get-disk and get-partition don't work if disk type dynamical.

  • I am pretty new to powershell. Why must you use -Confirm:$false instead just -Confirm? The documentation says that false is the default for confirm.  -Confirm $false also fails. What does the : do?

  • Hey people i have 2 ssd's, 1 with windows 7 and 1 with windows 8. + a raid 0 setup in my pc.
    The problem is, that when i switch the ssd's my raid setup does not automatically initialize...
    I was hoping to make some sort of script that runs on startup, that will automatically initialize my raid setup so that it can be used instandly...
    Can anyone help me out with this?
    Mail me at bram.donderwinkel@gmail.com

  • firstly, it's a good article, but i think is not good for normal user,
    there are huge 3rd part disk manage tool do this work,
    "marorit disk expert", "gparted", "acronis disk suites" and so on,
    most of these is friendly, and easy use, important is it hide complex
    technology which should not worth, and waste your time learning for normal user.

  • Ed, I'm seeing an odd behavior on WS2012R2 with this code. When the New-Partition cmdlet completes, Windows raises a dialog "You need to format the disk in drive D: before you can use it." It is instant, and is as if Windows itself is trying to access the disk before it is formatted. Even when your code is executed inside a pssession , the dialog gets raised for anyone unlucky enough to be in a console or RDP session on the remote server. Any thoughts?

  • Hi Scripting Guy
    A couple of my students have been trying to find a way to convert a basic til to a dynamic disk.
    They succeeded to do so using diskpart, however, when I challenged them to find a way to do it using PowerShell they gave up.
    I have not been able to find a usable cmdlet myself - is it possible to get a hint into the right direction?