DISKPART Script Fixes Default Win10 Disk Layout
In two previous two blog posts, Kari the Finn and I explained how to use DISKPART during Windows 10 installation. We also described what’s wrong with the default Windows disk layout, and why it benefits from this fix. This, the third and final installment in this series, puts those DISKPART commands into a batch file. In fact, this allows for easy automation, and explores an elaboration on this theme. First and foremost, though, readers will find that this DISKPART script fixes default Win10 disk layout issues quickly and easily.
Show Me How This DISKPART Script Fixes Default Win10 Disk Layout!
Basically, a DISKPART script is a text file with a .txt extension. Collecting all the DISKPART commands from Part 1, we simply add some remarks to document our work. Note: lines that start with rem are ignored when the script runs.
rem DISKPART script, put available space into C: rem --------------------------------------------------- rem Select Disk 0, wipe it empty, convert to GPT rem --------------------------------------------------- select disk 0 clean convert gpt rem --------------------------------------------------- rem Create & format 100 MB EFI System partition rem --------------------------------------------------- create partition efi size=100 format quick fs=fat32 label="System" rem --------------------------------------------------- rem Create 16 MB MSR partition (will not be formatted) rem --------------------------------------------------- create partition msr size=16 rem --------------------------------------------------- rem Create Windows partition using all available space rem --------------------------------------------------- create partition primary rem --------------------------------------------------- rem Shrink Windows partition to make space for WinRE rem --------------------------------------------------- shrink minimum=450 rem --------------------------------------------------- rem Format Windows partition, label it, no drive letter rem --------------------------------------------------- format quick fs=ntfs label="Windows" rem --------------------------------------------------- rem Create & format 450 MB recovery partition rem --------------------------------------------------- create partition primary format quick fs=ntfs label="WinRE" set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac" rem --------------------------------------------------- rem Exit Diskpart rem --------------------------------------------------- exit
Of course, you could omit the remark lines if you don’t want them. For ourselves, we always use remarks in ours script and batch files. That’s because it makes it easier to read and modify archived items like this one, long after we’ve forgotten all the intricate details. Here’s a download link to the script, should you wish to access it as such, rather than cut’n’paste the foregoing text.
DISKPART scripts run from the Command Prompt using this syntax diskpart /s Z:\MyScript.txt. In this command, Z: identifies the drive where the script resides. We save such scripts at the root of our install media. That means USB flash drives for physical installs, and in ISO files for Hyper-V virtual machines. Any available storage location will work. Thus, for instance, if you cannot add files to an install DVD, save the script on a flash drive instead.
Running the script
There’s one small issue with this script, though. Using GPT we don’t always know the drive letter for our install media or the media where the DISKPART script resides. In fact, those values depend on knowing if the disk is new, and all space unallocated, or if it is partitioned and formatted with drive letters assigned. In other words, YMMV.
That said, there’s an easy workaround. Before running the script, we need to know the install media’s volume label. That way, we can figure out which drive letter Windows setup assigns to that device. Knowing that, we can call the DISKPART script from the correct device.
We saved our DISKPART script as DiskConfig.txt on root of our Windows 10 PRO install media. This is a USB Flash drive labelled W10PRO_USB.
The following command elicits the drive letter for any medium (DVD, flash drive, HDD partition) labelled W10PRO_USB. Then it then runs the DISKPART script DiskConfig.txt. Note: this command is a one-liner, even though word wrap breaks across 2 lines:
for /f %X in (‘wmic volume get DriveLetter ^, Label ^|
find “W10PRO_USB”‘) do DISKPART /s %X\DiskConfig.txt
In this command string, notice the %X\ value before the script filename. Here, this does not refer to drive X:. Rather, it’s a variable that points to the media labeled W10PRO_USB. In fact, script variables are denoted with a single % sign at the command line. But you must use two % signs in batch files to make them work. That is, you’ll enter the text shown above that uses %X, but when you add that same command in a batch file, use %%X instead.
Script Output Illustrated
That’s it. When we boot Windows from our install media, a properly labeled flash drive must also be present (if the script is stored elsewhere). Then, we press SHIFT + F10 to open the Command Prompt. Finally, we enter the preceding command and let our partitioning script do its thing:
Here’s the output from the script as it runs.
[Click image for full-sized view; this version is hard to read!]
In under 30 seconds, our boot/system drive is partitioned just the way we like it! Here’s a screen cap to illustrate.
Here’s our disk layout, after the script is run. (This is for a VM with a deliberately small boot/system drive.)
Putting Additional partitions into the Mix
Here’s another common partitioning scenario to ponder. What if you have a 1 TB HDD or SSD? Then, you might want to allocate 200 GB to C:, and the rest to a data partition.
Actually, it’s quite simple. In fact, a few minor tweaks it’s all that needed. You merely add the size=xxx option for the Windows partition. Thus, you don’t allocate all available space to it. Ditto, add size=xxx to the WinRE partition, and place it after the Windows partition. In last place, create a data partition allocating all remaining space to it instead.
First, let’s change the Windows partition entries to allocate 200 GB instead of all available space.
create partition primary size=204800
format quick fs=ntfs label=”Windows”
Please note: DISKPART space allocation uses MB as its units. Thus, 200 GB = 200 x 1024 = 204800 MB. Here again, no drive letter is needed because Windows setup automatically assigns C: to this drive.
Next, let’s handle the recovery (WinRE) partition, and allocate it 450 MB:
create partition primary size=450
format quick fs=ntfs label=”WinRE”
set id=”de94bba4-06d1-4d40-a16a-bfd50179d6ac”
And finally, let’s add a data partition, and let it consume all remaining disk space:
create partition primary
format quick fs=ntfs label=”Data”
assign letter W
The Original Script, Revised
The script now looks like this:
rem DISKPART script, 200 GB Windows rest Data partition rem --------------------------------------------------- rem Select Disk 0, wipe it empty, convert to GPT rem --------------------------------------------------- select disk 0 clean convert gpt rem --------------------------------------------------- rem Create & format 100 MB EFI System partition rem --------------------------------------------------- create partition efi size=100 format quick fs=fat32 label="System" rem --------------------------------------------------- rem Create 16 MB MSR partition (will not be formatted) rem --------------------------------------------------- create partition msr size=16 rem --------------------------------------------------- rem Create & format a 200 GB Windows partition rem --------------------------------------------------- create partition primary size=204800 format quick fs=ntfs label="Windows" rem --------------------------------------------------- rem Create & format 450 MB recovery partition rem --------------------------------------------------- create partition primary size=450 format quick fs=ntfs label="WinRE" set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac" rem --------------------------------------------------- rem Create & format a data partition with letter W rem --------------------------------------------------- create partition primary format quick fs=ntfs label="Data" assign letter W rem --------------------------------------------------- rem Exit Diskpart rem ---------------------------------------------------
Note: we assigned drive W: to the data partition. Normally, this will be changed later once Windows is installed. For the moment, we want to be sure an unused letter is assigned. Otherwise, a letter collision could cause the script to fail.
As mentioned earlier, you can never be sure which drive letters are already assigned when booting from Windows install media on a UEFI / GPT system. Selecting a letter from the end of alphabet makes us pretty sure it’s free when the script runs. Otherwise, we intentionally never use letters W, Y and Z in our various Windows setups. Thus, we can be sure it’s safe to use these letters in scripts. Remember, X: is off-limits. That’s because X: is reserved for the Windows recovery console and the Windows PE Command Prompt when booting from install media or WinPE!