Windows 10

How to script SD card partitioning and imaging for IoT devices

Internet of Things conceptual image
Credit: Thinkstock

Last month, I discussed installing Windows 10 IoT Preview on the Raspberry Pi 2. The process entailed flashing a bootable image of Windows 10 onto an SD Card. Because Windows 10 Internet of Things is currently a preview version, Microsoft will probably release a new version very soon, most likely after Windows 10 desktop is made publicly available.

With that in mind, I thought it would be nice to be able to automate all the steps needed to update an existing SD card's OS image. This, of course, means deleting the old image and partitions, reformatting the card, and finally, writing the new OS image onto the card. 

So, I went ahead and did it, creating the script files listed below, and both are waiting, ready to be used (hopefully) soon to write the latest version of Microsoft's Internet of Things OS onto SD cards. 

Diskpart scripting

Diskpart supports the use of script files, specified by a /s parameter, like so:

diskpart /s somescriptfile.txt

For example, I created a new script file, sdcardclean.txt, redirecting diskpart's output to a log file, appropriately named logfile.txt.

diskpart /s sdcardclean.txt >logfile.txt

The section below lists every line contained in the sdcardclean.txt script file:

select disk 1
create partition primary
select partition 1
format fs=fat32 quick

You may notice the select disk 1 line, this is the number that is assigned to my SD card when no other USB drives or SD cards are attached to my Windows machine.

To view the number assigned to your card, use diskpart's list disk command, ensuring that it is the only external drive plugged into your machine. The following image shows how diskpart and list disk were used to find the number Windows assigned to an 8GB SD card (Disk 1): 

Using diskparts list disk command to find the drive number


Batch file

With the diskpart script out of the way, the only thing a batch file needs to run are two lines of code, the first to run the diskpart command, and a second command to flash the OS image onto a card.

For convenience, I created a folder, C:\images, and put three files inside: The sdcardclean.txt script file, the Windows 10 IoT flash.ffu image file, and a batch file, named createcard.bat

echo off
REM ***** Remove partitions and format *****
diskpart /s sdcardclean.txt >logfile.txt
REM ***** Flash Windows IoT to SDCard *****
dism.exe /Apply-Image /ImageFile:flash.ffu /ApplyDrive:\\.\PhysicalDrive1 /SkipPlatformCheck /Quiet
echo Finished applying image to SDCard

The following image shows the createcard batch file executing, running inside a command prompt window, opened with Administrator privileges:

SD card partitioning and formatting -- running createcard

Finally, you can validate Windows 10 IoT was written to the card, using the list partition command. Windows 10 IoT creates multiple partitions on a card, these partitions were earlier removed using diskpart's clear command, created anew when the newer flash.ffu image file was written to the card.

SD card partitioning and formatting -- validating card

These scripts and batch files are free for you to download and use, available on Github, here.

This article is published as part of the IDG Contributor Network. Want to Join?

ITWorld DealPost: The best in tech deals and discounts.