How can I remove all volumes from the hard drives in a WinPE environment using Diskpart through Powershell or WMIC calls? The WinPE image will be injected with the required packages using DISM beforehand, and I do not want to clear USB disks or any externally attached storage, including iSCSI links

Keep in mind this will be used as a part of a deployment environment in which the system is cleared before an image is applied. The deployment environment is intended to be completely unattended so the less stuff flying down screen, the better.

  • So you want to format all physical disks in a computer? Does that include connected USB disks?
    – Kinnectus
    Aug 25 '15 at 6:55
  • 1
    Last I checked, DISKPART is a Windows command and doesn't exist in MS-DOS?
    – user1686
    Aug 25 '15 at 6:55
  • @BigChris it does not include USB disks, I'll update that now
    – Bilfred
    Aug 25 '15 at 6:58
  • can you also update your question as to which technology you'd prefer to use... DISKPART cannot run under MS-DOS...
    – Kinnectus
    Aug 25 '15 at 7:28
  • 1
    Your question title originally had "MS-DOS" at the end - hence why grawity has also commented :) . You could write a script that you put on your WinPE disk to WMIC or PowerShell the local computer and identify the local physical disks. you can then use this output to create a DISKPART script to format the disks. The only caveat to WinPE is that it doesn't have WMIC or PowerShell support by default. You can add support and there are tutorials all over the place to achieve this - it's quite easy!
    – Kinnectus
    Aug 25 '15 at 8:06

To do the same through powershell you can use

Use with extreme caution, this script will delete hard drives!

(Get-Disk).where({$_.BusType -like "ATA"}) | Clear-Disk  -Confirm -Whatif -RemoveData -RemoveOEM

(I've put -Confirm -Whatif to be removed, so people can't randomly delete their entire comp! - remove it to delete your entire comp)

Obviously you need to make the "ATA" part whatever you need it to be, and you may need a confirm flag for the Clear-Disk (I didn't have a spare disk to test it on!)

You can use

get-disk | fl *

To get a list of all of the disks attached with all of the variables you can use to filter on.


(Answering the pre-edited question, when it asked about how to do this via command prompt / diskpart)

select disk 0
select disk 1

This will remove all partitions from the disks, and return them to raw devices.

If you select more disks than you know you'll have it will simply error on the non existent ones.

To then make it more selective you would need to write a script that would check the output of

select disk x
disk detail 

to check whether it is an ata disk etc then you can run the clean command if suitable

  • That's an obvious method, but will contain a fair amount of un-needed code, as at times I am expecting to refresh machines with upto 12 and above physical disks. If I were only expecting to use a few disks I may have considered it, and it also looks very ugly seeing a huge line of errors fly down the screen especially to unsuspecting users - this is an unattended deployment afterall. Hence why I'm looking down the path of using Powershell and WMIC. I should probably update the question with that too. Thanks for at least answering though.
    – Bilfred
    Aug 25 '15 at 10:01
  • Plus using powershell I'll have a reference to return to, so I know how many disks there are to even format/reformat/image/mount, etc
    – Bilfred
    Aug 25 '15 at 10:04
  • 1
    I was answering the pre-change question of a command prompt / diskpart ;) and as with any scripting language it is entirely possible to hide error messages, or remove them altogether by verifying what disks exist before hand - but that takes more elegant scripting
    – Michael B
    Aug 25 '15 at 10:16
  • True, and if I don't get any solid answers in the next few days I'll probably just use that method. I'm not terribly amazing at Powershell, but I'm trying to triangulate some google searching.
    – Bilfred
    Aug 25 '15 at 10:21

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.