{"id":2954,"date":"2013-08-30T00:01:00","date_gmt":"2013-08-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/08\/30\/automating-diskpart-with-windows-powershell-part-5\/"},"modified":"2013-08-30T00:01:00","modified_gmt":"2013-08-30T00:01:00","slug":"automating-diskpart-with-windows-powershell-part-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/automating-diskpart-with-windows-powershell-part-5\/","title":{"rendered":"Automating DiskPart with Windows PowerShell: Part 5"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use 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! Can we build a DISKPART script to automatically format USB drives as bootable?\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. It&rsquo;s Friday and Ed has had a long week (and a lot of feedback, I suspect, from my bad puns). So I suspect he&rsquo;ll be all &ldquo;tied up&rdquo; with email.\nWe have pulled together a really cool function called <strong>Get-DiskPartInfo<\/strong>, which automates the use of DiskPart to a point that&rsquo;s it&rsquo;s information is now an object that we can consume with Windows PowerShell.<strong> <\/strong>\n<strong>Note<\/strong> This is the final part in a series. If you are behind, please read:<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/08\/26\/automating-diskpart-with-windows-powershell-part-1.aspx\" target=\"_blank\">Automating DiskPart with Windows PowerShell: Part 1<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/08\/26\/automating-diskpart-with-windows-powershell-part-2.aspx\" target=\"_blank\">Automating DiskPart with Windows PowerShell: Part&nbsp;2<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/08\/26\/automating-diskpart-with-windows-powershell-part-3.aspx\" target=\"_blank\">Automating DiskPart with Windows PowerShell: Part&nbsp;3<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/08\/26\/automating-diskpart-with-windows-powershell-part-4.aspx\" target=\"_blank\">Automating DiskPart with Windows PowerShell: Part 4<\/a><\/li>\n<\/ul>\n<p>Let&rsquo;s look at a basic DiskPart script to make a USB key bootable again:<\/p>\n<p style=\"padding-left: 30px\">SELECT DISK 2<br \/> CLEAN<br \/> CREATE PARTITION PRIMARY<br \/> FORMAT FS=NTFS QUICK<br \/> ASSIGN<br \/> ACTIVE\nWith our current advanced function, we can already identify USB flash drives and hard drives. Because we can isolate them down to size, we can make a fairly educated guess about devices that are removable USB keys.\nEducated guess? Well, the one problem that I haven&rsquo;t been able to figure out an answer for is how to separate a hard drive USB device from a USB flash drive. That information is not presented in DiskPart.\nBut I can suggest that I think most of the USB flash drives I have are going to be under a certain size&hellip;let&rsquo;s say 32&nbsp;GB. And for my purposes (I would like to extend this to Microsoft Deployment Toolkit (MDT)&nbsp;2012), I can probably suggest that they won&rsquo;t be smaller than a certain size either&mdash;say 8&nbsp;GB.\nNow let&rsquo;s start building a new advanced function called <strong>Initialize-USBBoot<\/strong>. What we are going to do is build the script that is needed to make the keys bootable in DiskPart:<\/p>\n<p style=\"padding-left: 30px\">Function INITIALIZE-USBBOOT()<br \/> {<\/p>\n<p style=\"padding-left: 30px\">[cmdletbinding()]<br \/> Param()\nFirst, we&rsquo;re going to identify the parameters for our bootable devices: USB drives between 7.5&nbsp;GB and 65&nbsp;GB:<\/p>\n<p style=\"padding-left: 30px\">$TYPE=&rsquo;USB&rsquo;<br \/> $MIN=7GB<br \/> $MAX=65GB\nAnd now that we have a cool new way to parse DiskPart, this all gets so much easier:<\/p>\n<p style=\"padding-left: 30px\">$DRIVELIST=(GET-DISKPARTINFO | WHERE { $_.Type &ndash;eq $TYPE &ndash;and $_.DiskSize -lt $MAX and $_.DiskSize &ndash;gt $MIN })\nThis will return all drives that are seen by DiskPart, including their identified DiskID numbers, which we can use to build a single script for DiskPart.\nAgain, I&rsquo;m going with a &ldquo;simple is best&rdquo; approach when I build the content. First, I&rsquo;ll create the file for the DiskPart script:<\/p>\n<p style=\"padding-left: 30px\">NEW-ITEM -Path bootemup.txt -ItemType file -force | OUT-NULL\nThen I step through every drive in the list and obtain its DiskID from DiskPart:<\/p>\n<p style=\"padding-left: 30px\">$DRIVELIST | FOREACH { <br \/> $DISKNUM=$_.DISKNUM\nNow I&rsquo;ll build the script. Because it&rsquo;s simply a serial set of commands, we can build one script to do all the work:<\/p>\n<p style=\"padding-left: 30px\">ADD-CONTENT -Path bootemup.txt -Value &#8220;SELECT DISK $DiskNum&#8221; <br \/> ADD-CONTENT -Path bootemup.txt -Value &#8220;CLEAN&#8221; <br \/> ADD-CONTENT -Path bootemup.txt -Value &#8220;CREATE PARTITION PRIMARY&#8221; <br \/> ADD-CONTENT -Path bootemup.txt -Value &#8220;FORMAT FS=FAT32 QUICK&#8221; <br \/> ADD-CONTENT -Path bootemup.txt -Value &#8220;ASSIGN&#8221;<br \/> ADD-CONTENT -Path bootemup.txt -value &#8220;ACTIVE&#8221;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">}\nNow with this in place, I can run the following script:<\/p>\n<p style=\"padding-left: 30px\">INITIALIZE-USBBOOT<br \/> DISKPART \/S .BOOTEMUP.TXT\nNow we can plug-in a series of USB keys that fit those parameters and wipe them clean for booting!\nHow does MDT 2012 fit into all of this?\nLet&rsquo;s assume that you have a folder called C:DeploymentContent, and you need to be able to have a simple solution for technicians to build their keys&mdash;a solution that means consistency in the process.\nIn Windows PowerShell, we can launch Robocopy.exe like any other application, but also pass parameters to it. Because our new <strong>Get-DiskPartInfo<\/strong> cmdlet will also return the drive letter for those USB keys, we can identify our USB flash keys with those same parameters, and pass the results to Robocopy.exe. Here&rsquo;s a sample script that could meet this need:<\/p>\n<p style=\"padding-left: 30px\">$TYPE=&rsquo;USB&rsquo;<br \/> $MIN=7GB<br \/> $MAX=65GB<\/p>\n<p style=\"padding-left: 30px\">$DRIVELIST=(GET-DISKPARTINFO | WHERE { $_.Type &ndash;eq $TYPE &ndash;and $_.DiskSize -lt $MAX -and $_.DiskSize &ndash;gt $MIN })<\/p>\n<p style=\"padding-left: 30px\">$DRIVELIST | FOREACH {<\/p>\n<p style=\"padding-left: 30px\">$Source=&rdquo;C:DeploymentContent&rdquo;<br \/> $Destination=$_.DriveLetter<\/p>\n<p style=\"padding-left: 30px\">ROBOCOPY $Source $Destination \/E<\/p>\n<p style=\"padding-left: 30px\">}\nThere you have it! A bit of work to play with, but now we have an almost single-click solution to build those deployment keys. You could even leverage this to easily erase media keys and deploy documentation or client media.\nBy the way, if you don&rsquo;t feel like typing, this entire solution is uploaded as a module on the Script Center Repository:&nbsp;<a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Automate-Creation-of-798ea3dc#content\">Automate Creation of Bootable USB Keys with PowerShell<\/a>.\nAnd remember the choice is yours, as is the power&hellip;with Windows PowerShell!\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>&nbsp;(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<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Windows PowerShell to build scripts to automate DiskPart. &nbsp;Hey, Scripting Guy! Can we build a DISKPART script to automatically format USB drives as bootable? &mdash;SH &nbsp;Hello SH, Honorary Scripting Guy, Sean Kearney, here. I&rsquo;m filling in for our good friend, Ed Wilson. It&rsquo;s Friday and Ed has had a long week (and a [&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-2954","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: Use Windows PowerShell to build scripts to automate DiskPart. &nbsp;Hey, Scripting Guy! Can we build a DISKPART script to automatically format USB drives as bootable? &mdash;SH &nbsp;Hello SH, Honorary Scripting Guy, Sean Kearney, here. I&rsquo;m filling in for our good friend, Ed Wilson. It&rsquo;s Friday and Ed has had a long week (and a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2954","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=2954"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2954\/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=2954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}