{"id":2960,"date":"2013-08-29T00:01:00","date_gmt":"2013-08-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/08\/29\/automating-diskpart-with-windows-powershell-part-4\/"},"modified":"2013-08-29T00:01:00","modified_gmt":"2013-08-29T00:01:00","slug":"automating-diskpart-with-windows-powershell-part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/automating-diskpart-with-windows-powershell-part-4\/","title":{"rendered":"Automating DiskPart with Windows PowerShell: Part 4"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use Windows PowerShell to build scripts to automate DiskPart.<\/p>\n<p><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 use Windows PowerShell to return information from DiskPart as an object?<\/p>\n<p>&mdash;SH<\/p>\n<p><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,<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, here. I&rsquo;m filling in for our good friend, Ed Wilson. He&rsquo;s got tie&rsquo;s everywhere! Ties all over his property! The problem is they&rsquo;re running amok! You see, he&rsquo;s turned into a &ldquo;tie&rdquo; fighter (and may the farce be with you).<\/p>\n<p>Today we&rsquo;re going to pull all of this together into a function that we&rsquo;re going to call <strong>Get-DiskPartInfo<\/strong>. We will pull all of these scripts together to automatically parse through the disks in DiskPart, pull their data, and return it as a <strong>[pscustomobject]<\/strong>.<\/p>\n<p><strong>Note<\/strong>&nbsp;&nbsp;&nbsp;This is Part 4 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<\/ul>\n<p>So shall we get started? First we define our function, but we&rsquo;ll attach the appropriate parameters and make this a true advanced function:<\/p>\n<p style=\"padding-left: 30px\">Function GET-DISKPARTINFO() {<\/p>\n<p style=\"padding-left: 30px\">[cmdletbinding()]<\/p>\n<p style=\"padding-left: 30px\">Param()<\/p>\n<p>Now let&rsquo;s add our first DiskPart script to get the number of drives:<\/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;<br \/> $LISTDISK=(DISKPART \/S LISTTDISK.TXT)<br \/> $TOTALDISK=($LISTDISK.Count)-9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>Then we can loop through each disk, grabbing the <strong>DiskID<\/strong> and its physical size:<\/p>\n<p style=\"padding-left: 30px\">for ($d=0;$d -le $TOTALDISK;$d++)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">$SIZE=$LISTDISK[-1-$d].substring(25,9).replace(&#8221; &#8220;,&#8221;&#8221;) <br \/> $DISKID=$LISTDISK[-1-$d].substring(7,5).trim()<\/p>\n<p>Now that we have the <strong>DiskID<\/strong>, we can write a simple script for DiskPart to call up that disk and grab it&rsquo;s <strong>Detail<\/strong>:<\/p>\n<p style=\"padding-left: 30px\">NEW-ITEM -Name detail.txt -ItemType file -force | OUT-NULL <br \/> ADD-CONTENT -Path detail.txt &#8220;SELECT DISK $DISKID&#8221; <br \/> ADD-CONTENT -Path detail.txt &#8220;DETAIL DISK&#8221;<br \/> $DETAIL=(DISKPART \/S DETAIL.TXT)<\/p>\n<p>And now with the <strong>Detail<\/strong> for that disk in hand, we run through our <strong>Detail<\/strong> parsing to pull the information we need from there:<\/p>\n<p style=\"padding-left: 30px\">$MODEL=$DETAIL[8] <br \/> $TYPE=$DETAIL[10].substring(9) <br \/> $DRIVELETTER=$DETAIL[-1].substring(15,1)<\/p>\n<p>Now we take a few minutes to convert that drive size to a real integer that we can use to compare the size of the removable disk:<\/p>\n<p style=\"padding-left: 30px\">$LENGTH=$SIZE.length<br \/> $MULTIPLIER=$SIZE.substring($length-2,2) <br \/> $INTSIZE=$SIZE.substring(0,$length-2)<\/p>\n<p style=\"padding-left: 30px\">SWITCH($MULTIPLIER) <br \/> &nbsp;{ <br \/> &nbsp; KB { $MULT = 1KB } <br \/> &nbsp; MB { $MULT = 1MB } <br \/> &nbsp; GB { $MULT = 1GB } <br \/> &nbsp;}<\/p>\n<p style=\"padding-left: 30px\">$DISKTOTAL=([convert]::ToInt16($INTSIZE,10))*$MULT<\/p>\n<p>Then all we need do now is wrap it up neatly as a custom object in Windows PowerShell:<\/p>\n<p style=\"padding-left: 30px\">[pscustomobject]@{DiskNum=$DISKID;Model=$MODEL;Type=$TYPE;DiskSize=$DISKTOTAL;DriveLetter=$DRIVELETTER} <br \/> } <br \/> }<\/p>\n<p>Pull this advanced function together and you can turn the following information&hellip;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6523.4.1.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6523.4.1.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>&hellip;into useful output like the following in Windows PowerShell:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7713.4.2.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7713.4.2.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Now we can even do something like this:<\/p>\n<p style=\"padding-left: 30px\">GET-DISKPARTINFO | WHERE { $_.Type &ndash;eq &lsquo;USB&rsquo; &ndash;and $_.DiskSize -lt16GB }<\/p>\n<p>Here&rsquo;s where things are going to be interesting tomorrow. With all this information as an object, I&rsquo;ll bet you&rsquo;re wondering if I can go the next level, and automatically format those USB keys. Check in tomorrow for our final segment where we&rsquo;ll tie all of this into that and automatically build a key from Microsoft Deployment Tootkit (MDT) 2012!<\/p>\n<p>I 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=\"mailto: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.<\/p>\n<p><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 use Windows PowerShell to return information from DiskPart as an object? &mdash;SH &nbsp;Hello SH, Honorary Scripting Guy, Sean Kearney, here. I&rsquo;m filling in for our good friend, Ed Wilson. He&rsquo;s got tie&rsquo;s everywhere! Ties all over his property! The problem [&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-2960","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 use Windows PowerShell to return information from DiskPart as an object? &mdash;SH &nbsp;Hello SH, Honorary Scripting Guy, Sean Kearney, here. I&rsquo;m filling in for our good friend, Ed Wilson. He&rsquo;s got tie&rsquo;s everywhere! Ties all over his property! The problem [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2960","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=2960"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2960\/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=2960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}