I'm trying to write a script to create and format a partition in Windows Server 2008R2.

Now, when disk 1 is selected, I need to format it, only if it is not formatted already. This is what I have now:

Run: diskpart /s script.txt

Content of script.txt

select disk 1
create partition primary
format fs=ntfs unit=65536 quick
assign letter=D

Any help?

  • You should be using Powershell as opposed to using DOS commands, Powershell allows for greater flexibility and will allow you to evaluate if a disk is not formatted then perform an action on it if the statement equals true. I assume you already know this though as you added a "Powershell" tag into your post. In any case, this command should get you started "Get-Command disk" – Mbond65 Aug 5 '14 at 11:36
  • You are right, the problem its that in Windows 2008R2 there are no Storage Cmdlets in Windows PowerShell. – ccamacho Aug 5 '14 at 13:19
  • Those diskpart replacements are only for v4 powershell and above. But don't worry, we can still check drive formatted status via Get-WmiObject cmdlet. Also look into the Win32_Volume Format method to avoid diskpart. – Knuckle-Dragger Aug 5 '14 at 16:36

This works for me.

foreach ($disk in get-wmiobject Win32_DiskDrive -Filter "Partitions = 0"){ 
   "select disk "+$disk.Index+"`r clean`r create partition primary`r format fs=ntfs unit=65536 quick`r active`r assign letter=D" | diskpart

In this case, i get the disk with NO partitions, and then i create a D drive with all available space


I did this a while back using here strings to format the complete diskpart commands. It's a bit goofy looking but it worked at the time when we were trying to automate this quickly for a high number of luns on multi node clusters. the WMI method is definitely cleaner looking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.