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

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:


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, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (18)

  1. HyperV_Guy says:

    @mnscripter, this is an odd windows default behaviour. i recommend doing a Stop-Service ShellHWDetection prior the new-partition command and start-service ShellHWDetection right after the format-volume !
    Cheers, @HyperV_Guy

  2. MNscripter says:

    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?

  3. Anonymous says:

    @mnscripter, I was seeing the same issue. I wrote the script out longer instead of trying to do it in one long pipe and it is working. I was hoping to get @Ed’s to work.

    $VolumeNumber = (Get-Disk | where {$_.partitionstyle -eq ‘raw’}).Number
    Clear-Disk -Number $VolumeNumber -RemoveData -RemoveOEM -confirm:$false
    New-Partition -DiskNumber $VolumeNumber -UseMaximumSize -IsActive:$true -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "disk2" -Confirm:$false

  4. fruitfruit says:

    awesome, no mucking around with diskpart!

  5. mumtaz says:

    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 ?

  6. dmJoker says:

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

  7. Dave says:

    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?

  8. Bram Thunderstore says:

    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

  9. trik.k says:

    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.

  10. Orla Søe says:

    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?

  11. Wes says:

    where is the link to the actual script?

  12. Creed says:

    It works but once and a while it will give the error "Format-Volume : Cannot perform the requested operation when the drive is read only" seems like it tries to format before the new partition is finished being created. This is on VM’s did not test on

  13. Pyrochaser says:

    @Creed – What if you put a Start-sleep in the command? Example:
    $VolumeNumber = (Get-Disk | where {$_.partitionstyle -eq ‘raw’}).Number
    Clear-Disk -Number $VolumeNumber -RemoveData -RemoveOEM -confirm:$false
    New-Partition -DiskNumber $VolumeNumber -UseMaximumSize -IsActive:$true -AssignDriveLetter | Start-Sleep 10 | Format-Volume -FileSystem NTFS -NewFileSystemLabel "disk2" -Confirm:$false

    I haven’t tested it yet, I don’t think that piping into a start-sleep followed by the format-volume will work but it’s worth a try

  14. Pyrochaser says:


    I was able to figure out how to do it a little more carefully. In order for this script to run it must be run on server 2012 as well as being elevated with Admin Permissions. Once elevated the script will check for new raw HDD and make them visible to the system.
    In order to prevent the Format Disk prompt windows pops when a new drive is detected I stopped the Hardware Detection service, once the script completes it starts it again.

    Script starts below:

    ### Stops the Hardware Detection Service ###
    Stop-Service -Name ShellHWDetection

    ### Grabs all the new RAW disks into a variable ###
    $disk = get-disk | where partitionstyle -eq ‘raw’

    ### Starts a foreach loop that will add the drive
    ### and format the drive for each RAW drive
    ### the OS detects ###
    foreach ($d in $disk){
    $diskNumber = $d.Number
    $dl = get-Disk $d.Number | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize
    Format-Volume -driveletter $dl.Driveletter -FileSystem NTFS -NewFileSystemLabel "OS Disk $diskNumber" -Confirm:$false
    ### 2 Second pause between each disk ###
    ### Initialization, Partitioning, and formatting ###
    Start-Sleep 2
    ### Starts the Hardware Detection Service again ###
    Start-Service -Name ShellHWDetection

    ### end of script ###

    I hope this helps. Works pretty successfully on my virtual 2012 Servers once I add a drive to the VM.

  15. Pyrochaser says:


    If the format collides with the Partitioning still you could add a start-sleep 5 just after the line for New-Partition and just before the line for Format-Volume.

  16. Darrick West says:

    Why no examples of creating a GPT layout with EFS, MSR, and Primary partitions?

  17. Darrick West says:

    This is what I came up with, but I can’t format the EFS partition:

    #Clear Disk
    Clear-Disk -Number 0 -RemoveData -RemoveOEM -Confirm:$false

    #Initialize Disk
    Initialize-Disk -Number 0 -PartitionStyle GPT

    #GPT EFS
    $pEFS = New-Partition -DiskNumber 0 -Size 512MB -GptType ‘{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}’ -AssignDriveLetter
    #$pEFS | fl -Property *
    Format-Volume -DriveLetter $($pEFS.DriveLetter) -FileSystem FAT32 -NewFileSystemLabel EFS -Confirm:$false -Force

    #GPT MSR
    New-Partition -DiskNumber 0 -Size 128MB -GptType ‘{e3c9e316-0b5c-4db8-817d-f92df00215ae}’
    #New-Partition -DiskNumber 0 -Size 128MB -GptType MSR

    #GPT OSDisk
    $pOSDisk = New-Partition -DiskNumber 0 -UseMaximumSize -GptType ‘{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}’ -AssignDriveLetter
    Format-Volume -DriveLetter $($pOSDisk.DriveLetter) -FileSystem NTFS -NewFileSystemLabel EFS -Confirm:$false -Force

  18. Bink says:

    I ran into a similar issue with the read only nonsense and resolved it with something like the following:

    New-Partition … | ForEach-Object { Start-Sleep -s 3; $_ | Format-Volume … }

Skip to main content