{"id":75331,"date":"2015-12-02T00:01:00","date_gmt":"2015-12-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/12\/02\/build-a-powershell-enabled-windows-pe-key-part-3\/"},"modified":"2019-02-18T09:20:49","modified_gmt":"2019-02-18T16:20:49","slug":"build-a-powershell-enabled-windows-pe-key-part-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/build-a-powershell-enabled-windows-pe-key-part-3\/","title":{"rendered":"Build a PowerShell-Enabled Windows PE Key: Part 3"},"content":{"rendered":"<p><b>Summary<\/b>: Identify available USB keys and make them bootable devices.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here to continue delving into having a cool PowerShell experience in a Windows PE environment. Today I&rsquo;m going to show you how to use Windows PowerShell to identify USB keys and make them bootable devices.<\/p>\n<p><b>&nbsp; &nbsp;<\/b><strong>Note<\/strong>&nbsp;&nbsp; This is a five-part series that includes the following posts:<\/p>\n<ul>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/11\/30\/build-a-powershell-enabled-windows-pe-key-part-1.aspx\" target=\"_blank\">Build a PowerShell-Enabled Windows PE Key: Part 1<\/a><br \/>Install the Windows ADK and validate its presence with Windows PowerShell<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/12\/01\/build-a-powershell-enabled-windows-pe-key-part-2.aspx\" target=\"_blank\">Build a PowerShell-Enabled Windows PE Key: Part 2<\/a><br \/>Customize a Windows PE environment to contain Windows PowerShell and DISM modules<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/12\/02\/build-a-powershell-enabled-windows-pe-key-part-3.aspx\" target=\"_blank\">Build a PowerShell-Enabled Windows PE Key: Part 3<\/a><br \/>Identify available USB keys and make them bootable devices<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/12\/03\/build-a-powershell-enabled-windows-pe-key-part-4.aspx\" target=\"_blank\">Build a PowerShell-Enabled Windows PE Key: Part 4<\/a><br \/>Build out the necessary file structure for a Windows PE environment<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/12\/04\/build-a-powershell-enabled-windows-pe-key-part-5.aspx\" target=\"_blank\">Build a PowerShell-Enabled Windows PE Key: Part 5<\/a><br \/>Populate a bootable USB key with content from Windows PE<\/li>\n<\/ul>\n<p>With Windows PowerShell finding a USB key is actually quite easy. We just use the <b>Get-Disk<\/b> cmdlet. This will show us all disks on the computer. There is a property we can filter on called <b>BusType<\/b>. A USB key is on the <b>BusType<\/b> called <b>USB<\/b>, conveniently enough. We can filter it in this way:<\/p>\n<p style=\"margin-left:30px\">Get-Disk | Where-Object { $_.BusType &ndash;eq &lsquo;USB&rsquo; }<\/p>\n<p>Now let&rsquo;s play my favorite game, &ldquo;what if.&rdquo; What if there was more than one USB key or drive attached? How could you choose?<\/p>\n<p>We can pipe the output to <b>Out-Gridview<\/b> and use the <b>&ndash;passthru<\/b> parameter. This will output a simple list of all the USB keys, including their sizes, to allow you to choose.<\/p>\n<p style=\"margin-left:30px\">Get-Disk | Where-Object { $_.BusType &ndash;eq &lsquo;USB&rsquo; } | Out-Gridview &ndash;passthru<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8475.1.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8475.1.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now that we have an easy way to select the USB keys, we can store this away in a PowerShell object we&rsquo;ll call <b>$Disk<\/b>:<\/p>\n<p style=\"margin-left:30px\">$Disk= Get-Disk | Where-Object { $_.BusType &ndash;eq &lsquo;USB&rsquo; } | Out-Gridview &ndash;passthru<\/p>\n<p>The next task is to clear away any partitions on the key. Normally <b>Clear-Disk<\/b> should work, but I&rsquo;ve experienced some issues in which I&rsquo;ve had to purge the partition table first. To properly eradicate all data on the USB key, I use a combination of <b>Clear-Disk<\/b> and <b>Remove-Partition<\/b>. I add <b>SilentlyContinue<\/b> to <b>ErrorAction<\/b> in case the key has already been purged.<\/p>\n<p style=\"margin-left:30px\">Get-Disk -Number ($Disk.number) | Get-Partition | Remove-partition -confirm:$false -ErrorAction SilentlyContinue<br \/> Clear-Disk -Number $Disk.Number -RemoveData -RemoveOEM -confirm:$false -ErrorAction SilentlyContinue<\/p>\n<p>Now that the key has been purged of data, we can begin creating a bootable key. We&rsquo;re going to assign a temporary drive letter of Z to the USB key.<\/p>\n<p>We first initialize the USB key we selected earlier and stored in <b>$Disk<\/b>:<\/p>\n<p style=\"margin-left:30px\">Initialize-Disk -Number $Disk.Number -PartitionStyle MBR -ErrorAction SilentlyContinue<\/p>\n<p>We then create a single partition to store the data. The <b>&ndash;UseMaximum<\/b> parameter will assign all available space to the partition on the USB key. <b>&ndash;IsActive<\/b> marks it as an active partition for booting.<\/p>\n<p style=\"margin-left:30px\">$OSDrive=&rsquo;Z&rsquo;<br \/>$Partition=New-Partition -DiskNumber $Disk.Number -DriveLetter $OSDrive -UseMaximumSize &ndash;IsActive<\/p>\n<p>We format this as <b>Fat32<\/b> because we our planning for the ability of booting in a modern UEFI platform. UEFI does not recognize NTFS as a filesystem for booting.<\/p>\n<p style=\"margin-left:30px\">Format-Volume -Partition $Partition -FileSystem FAT32 -NewFileSystemLabel &#039;Windows&#039;<\/p>\n<p>Now we have the ability to create a bootable USB key with a selectable menu. Neat, eh?<\/p>\n<p>Pop in tomorrow as we look into starting to build out the file structure needed for the Windows PE environment!<\/p>\n<p>I invite you to follow the Scripting Guys 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 them 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 always remember that with great PowerShell comes great responsibility.<\/p>\n<p><b>Sean Kearney<\/b>, Honorary Scripting Guy, Cloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Identify available USB keys and make them bootable devices. Honorary Scripting Guy, Sean Kearney, is here to continue delving into having a cool PowerShell experience in a Windows PE environment. Today I&rsquo;m going to show you how to use Windows PowerShell to identify USB keys and make them bootable devices. &nbsp; &nbsp;Note&nbsp;&nbsp; This is [&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,3,154,649,45],"class_list":["post-75331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-scripting-guy","tag-sean-kearney","tag-windows-pe","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Identify available USB keys and make them bootable devices. Honorary Scripting Guy, Sean Kearney, is here to continue delving into having a cool PowerShell experience in a Windows PE environment. Today I&rsquo;m going to show you how to use Windows PowerShell to identify USB keys and make them bootable devices. &nbsp; &nbsp;Note&nbsp;&nbsp; This is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75331","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=75331"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75331\/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=75331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=75331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=75331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}