{"id":2972,"date":"2013-08-26T00:01:00","date_gmt":"2013-08-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/08\/26\/automating-diskpart-with-windows-powershell-part-1\/"},"modified":"2013-08-26T00:01:00","modified_gmt":"2013-08-26T00:01:00","slug":"automating-diskpart-with-windows-powershell-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/automating-diskpart-with-windows-powershell-part-1\/","title":{"rendered":"Automating DiskPart with Windows PowerShell: Part 1"},"content":{"rendered":"<p><strong>Summary<\/strong>: Honorary Scripting Guy, Sean Kearney, talks about using Windows Powershell to build scripts to automate DiskPart.\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! I know I can create a bootable USB key by using DiskPart, but is there any way to have it automated so the computer does the work for me?\n&mdash;SH\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello SH,\nHonorary Scripting Guy, Sean Kearney, here. I&rsquo;m filling in for our good friend, Ed Wilson, who&rsquo;s building some giant contraption in his backyard to hold his giant collection of bow ties. So he&rsquo;s a bit &ldquo;tied up.&rdquo;\nSo the answer is absolutely, &ldquo;Yes! Yes! Yes!&rdquo; You can automate the creation of bootable memory keys. Having this ability is great if you&rsquo;re using utilities like MDT&nbsp;2012, and you need bootable media on the fly.\nSo I sat down and poked through my shiny new Surface Pro to dig for cmdlets to automate creating bootable partitions on USB. &nbsp;There are so many new modules and cmdlets to choose from.\nThen my jaw dropped. There were none (at least none that I could spy), and I wanted to fix that.\nCreating a bootable USB key is pretty easy. Most of us could run this process with our hands behind our backs while singing a happy tune. The standard procedure for a bootable USB key is:<\/p>\n<ol>\n<li>Launch <strong>DiskPart<\/strong> as an Administrator.<\/li>\n<li>Use <strong>List Disk<\/strong> to find your USB key (or keys if you&rsquo;re being efficient).<\/li>\n<li>Use <strong>Select Disk<\/strong> to choose your USB key.<\/li>\n<li><strong>Clean<\/strong> (to wipe out the MBR and delete the partition entries).<\/li>\n<li>Run <strong>Create Partition Primary<\/strong> to create a new primary partition on the USB flash drive.<\/li>\n<li><strong>Format FS=FAT32 Quick<\/strong> to lay down the file system as Fat32.<\/li>\n<li><strong>Assign<\/strong> (to give our friend a drive letter).<\/li>\n<li><strong>Active<\/strong> to make it bootable.<\/li>\n<\/ol>\n<p>So if your USB key always came up as DISK 2, you might have a little text file called &ldquo;bootme.txt&rdquo; with the following commands:<\/p>\n<ol>\n<li><strong>Select DISK 2<\/strong>.<\/li>\n<li><strong>Clean<\/strong>.<\/li>\n<li><strong>Create Partition Primary<\/strong>.<\/li>\n<li><strong>Format FS=FAT32 Quick<\/strong>.<\/li>\n<li><strong>Assign<\/strong>.<\/li>\n<li><strong>Active<\/strong>.<\/li>\n<\/ol>\n<p>You could happily run:<\/p>\n<p style=\"padding-left: 30px\">DISKPART \/s .bootme.txt\n&hellip;.and whoosh! All done.\nBut wouldn&rsquo;t it be far nicer to have something a bit more dynamic with the ability to figure out most of the work for you? Then you actually could do this with a touch of your fingers!\nThe ironic thing about DiskPart is that it works great, but it doesn&rsquo;t have a native way to export the information it has.\nBut I know of something that can help. Wait for it&hellip;\nStarts with a &ldquo;P&rdquo;&hellip;\nHas a cool theme song&hellip;\nYes! Of course! PowerShell can aid here! We can actually have Windows PowerShell create scripts for DiskPart and leverage the output of DiskPart to create newer scripts for it!\nSounds like magic doesn&rsquo;t it? Nope. Just a feature of Windows PowerShell that has been used before to automate applications like HANDLE.exe from Sysinternals.\nSo our first challenge is to create a text file for DiskPart to literally &ldquo;List the disks.&rdquo;\nWe could cheat and simply type something in Notepad, but if we use a script (or better yet, build a function), the solution will be self-contained.\nSo first&mdash;oh mighty PowerShell&mdash;make me a text file. We could do something really fancy and elegant, but sometimes simplicity is best. Simple means easy to modify and easy to understand. Here we go:<\/p>\n<p style=\"padding-left: 30px\">NEW-ITEM &ndash;name listdisk.txt &ndash;itemtype file &ndash;force | OUT-NULL<br \/> ADD-CONTENT &ndash;path listdisk.txt &ldquo;LIST DISK&rdquo;\nNow we can automate DiskPart with this new mini script. But we&rsquo;ll want to capture the output:<\/p>\n<p style=\"padding-left: 30px\">$LISTDISK=(DISKPART \/S LISTDISK.TXT)&nbsp;&nbsp;&nbsp;\nSo what we&rsquo;ve done here is capture all the output DiskPart was sending to the screen as a Windows PowerShell object. Now we can do all sorts of really neat stuff&mdash;like parse the data for what we want.\nSo the first thing we need to know from &ldquo;LIST DISK&rdquo; is, well, how many disks we&rsquo;re looking at. Believe it or not, this is easier than you imagine. First off, the number of lines in the output is a built-in property:<\/p>\n<p style=\"padding-left: 30px\">$TOTALDISK=$LISTDISK.count\nNow there are a lot of funky ways to figure this out. But honestly, all we have to know is how many of the lines in the output do not contain the information we want&mdash;the lines with the disk information. That number happens to be 9. (Yes, I smudged up my screen and counted 1&hellip;.2&hellip;..3&hellip;.ketchup stain.)\nSo we can use Windows PowerShell to calculate this:<\/p>\n<p style=\"padding-left: 30px\">$TOTALDISK=($LISTDISK.count)-9\nBut we have a ways to go. We need to find out the assigned number for the drive if it&rsquo;s a USB flash drive&hellip;whether it&rsquo;s pink or green.\nBut we&rsquo;ll come back tomorrow and show more about how we can parse the information from DiskPart!\nI invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to Ed at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Sean Kearney<\/strong> (filling in for our good friend Ed Wilson),<br \/>&nbsp; &nbsp; &nbsp; Honorary Scripting Guy, Windows PowerShell MVP<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&hellip;and good personal friend of the BATCHman\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Honorary Scripting Guy, Sean Kearney, talks about using Windows Powershell to build scripts to automate DiskPart. &nbsp;Hey, Scripting Guy! I know I can create a bootable USB key by using DiskPart, but is there any way to have it automated so the computer does the work for me? &mdash;SH &nbsp;Hello SH, Honorary Scripting Guy, [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,2,3,4,154],"class_list":["post-2972","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney"],"acf":[],"blog_post_summary":"<p>Summary: Honorary Scripting Guy, Sean Kearney, talks about using Windows Powershell to build scripts to automate DiskPart. &nbsp;Hey, Scripting Guy! I know I can create a bootable USB key by using DiskPart, but is there any way to have it automated so the computer does the work for me? &mdash;SH &nbsp;Hello SH, Honorary Scripting Guy, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2972"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2972\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}