{"id":73061,"date":"2015-09-29T00:01:00","date_gmt":"2015-09-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/09\/29\/use-powershell-to-create-windows-to-go-keyspart-2\/"},"modified":"2019-02-18T09:35:05","modified_gmt":"2019-02-18T16:35:05","slug":"use-powershell-to-create-windows-to-go-keyspart-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-windows-to-go-keyspart-2\/","title":{"rendered":"Use PowerShell to Create Windows To Go Keys&#8212;Part 2"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Use Windows PowerShell to partition a Windows To Go device.<\/span><span style=\"font-size:12px\"><\/span><\/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! Am I able to use Windows PowerShell to set up the necessary partitions to prepare a Window To Go key?<\/p>\n<p>&mdash;LL<\/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 LL,<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here. I&#8217;m continuing with my little chat about setting up Windows To Go keys through Windows PowerShell.<\/p>\n<p><b>&nbsp; &nbsp;Note<\/b>&nbsp;&nbsp;This is a five-part series that includes the following posts:<\/p>\n<ul>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/09\/28\/use-powershell-to-create-windows-to-go-keys-part-1.aspx\" target=\"_blank\">Use PowerShell to Create Windows To Go Keys&mdash;Part 1<\/a>&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; Use Windows PowerShell to identify Windows To Go devices.<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/09\/29\/use-powershell-to-create-windows-to-go-keys-part-2.aspx\" target=\"_blank\">Use PowerShell to Create Windows To Go Keys&mdash;Part 2<\/a>&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; Use Windows PowerShell to partition a Windows To Go device.<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/09\/30\/use-powershell-to-create-windows-to-go-keys-part-3.aspx\" target=\"_blank\">Use PowerShell to Create Windows To Go Keys&mdash;Part 3<\/a>&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; Use Windows PowerShell to apply an image to a Windows To Go key and make it bootable.<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/10\/01\/use-powershell-to-create-windows-to-go-keys-part-4.aspx\" target=\"_blank\">Use PowerShell to Create Windows To Go Keys&mdash;Part 4<\/a>&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp; &nbsp;Use Windows PowerShell to inject drivers and populate the data for Unattend.xml.<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2015\/10\/02\/use-powershell-to-create-windows-to-go-keys-part-5.aspx\" target=\"_blank\">Use PowerShell to Create Windows To Go Keys&mdash;Part 5<\/a>&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; Use a basic Windows PowerShell workflow to create multiple devices.<\/li>\n<\/ul>\n<p>Yesterday, I talked about how to use the <strong>Get-Disk<\/strong> cmdlet to identify keys. Today I&#8217;m going to use that information to prepare the necessary partition structure.<\/p>\n<p>The first thing I&rsquo;m going to do is erase everything that is on the key and purge its previous partitions.<\/p>\n<p>For this, I can use the <b>Clear-Disk<\/b> cmdlet to erase the key. For this cmdlet to work, it needs something specific to target. I could target the friendly name, but I&rsquo;m going to be very specific and target the disk number, which is unique to the device I want to format.<\/p>\n<p>Yesterday I captured the information from the <b>Get-Disk<\/b> cmdlet to <b>$WTG<\/b>:<\/p>\n<p style=\"margin-left:30px\"><span style=\"font-size:12px\">$DiskNumber=$WTG.DiskNumber<\/span><\/p>\n<p>Now I&rsquo;ll use the <b>Clear-Disk<\/b> cmdlet in the following manner to purge the data on this key. Normally, I would be prompted by PowerShell, so I&rsquo;ll bypass this by setting the <b>Confirm<\/b> switch to <b>$False<\/b>:<\/p>\n<p style=\"margin-left:30px\">Clear-Disk &ndash;Number $DiskNumber &ndash;RemoveData &ndash;RemoveOEM &ndash;Confirm:$False<\/p>\n<p>For some reason, I have occasionally encountered an issue where <b>Clear-Disk<\/b> can&rsquo;t seem to remove the partitions. So I can do some due diligence and use <b>Get-Partition<\/b> and <b>Remove-Partition<\/b> first:<\/p>\n<p style=\"margin-left:30px\">Get-Disk &ndash;number $DiskNumber | Get-Partition | Remove-Partition &ndash;confirm:$False<\/p>\n<p>There! Much cleaner now.<\/p>\n<p>Now I use the <b>Initialize-Disk<\/b> cmdlet to set up this device for partitions:<\/p>\n<p style=\"margin-left:30px\">Initialize-Disk &ndash;Number $DiskNumber &ndash;PartitionStyle MBR<\/p>\n<p>I can now start preparing two partitions: the 350 Megabyte System Partition and whatever remains for the operating system.<\/p>\n<p>I&#8217;ll use the <b>New-Partition<\/b> cmdlet to create these. Because I&rsquo;ll need to reference them afterwards for formatting, I&rsquo;ll store the objects in PowerShell variables:<\/p>\n<p style=\"margin-left:30px\">$System=New-Partition -DiskNumber $DiskNumber -size (350MB) &ndash;IsActive<\/p>\n<p style=\"margin-left:30px\">$OS= New-Partition &ndash;DiskNumber $DiskNumber &ndash;UseMaximumSize<\/p>\n<p>Now I can format the two partitions by using the <b>Format-Partition<\/b> cmdlet. I need to supply the objects I previously captured when I created the partitions:<\/p>\n<p style=\"margin-left:30px\">Format-Volume -NewFileSystemLabel &#8220;System&#8221; -FileSystem FAT32 -Partition $System -confirm:$False<\/p>\n<p style=\"margin-left:30px\">Format-Volume -NewFileSystemLabel &#8220;Windows&#8221; -FileSystem NTFS -Partition $OS -confirm:$False<\/p>\n<p>I assign each of the drives a letter so I can interact with them later. I&#8217;ll use <b>Y<\/b> for the system drive and <b>Z<\/b> for the operating system drive. &nbsp;(You can adjust the script to meet your needs.)<\/p>\n<p style=\"margin-left:30px\">$DriveSystem=&rsquo;Y&rsquo;<\/p>\n<p style=\"margin-left:30px\">$DriveOS=&rsquo;Z&rsquo;<\/p>\n<p style=\"margin-left:30px\">Set-Partition -InputObject $System -NewDriveLetter $DriveSystem<\/p>\n<p style=\"margin-left:30px\">Set-Partition -InputObject $OS -NewDriveLetter $DriveOS<\/p>\n<p>One of the most critical pieces is setting the Windows To Go operating system so that if it&rsquo;s attached to a computer and viewed from a foreign operating system, it does not automatically get assigned a drive letter. This gives it some rudimentary protection from having its data viewable by a foreign operating system. Using Bitlocker To Go later would be a good addition.<\/p>\n<p style=\"margin-left:30px\">Set-Partition -InputObject $OS &ndash;NoDefaultDriveLetter<\/p>\n<p>At this point, I have the key ready for my next step, which is to lay down a Windows image. I&#8217;ll be getting that done tomorrow.<\/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 an email to The Scripting Guys 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, remember to eat your cmdlets every day with a dash of creativity.<\/p>\n<p><b>Sean Kearney, <\/b>Windows PowerShell MVP and Honorary Scripting Guy<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Windows PowerShell to partition a Windows To Go device. &nbsp;Hey, Scripting Guy! Am I able to use Windows PowerShell to set up the necessary partitions to prepare a Window To Go key? &mdash;LL &nbsp;Hello LL, Honorary Scripting Guy, Sean Kearney, is here. I&#8217;m continuing with my little chat about setting up Windows To [&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,154,45,616],"class_list":["post-73061","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell","tag-windows-to-go"],"acf":[],"blog_post_summary":"<p>Summary: Use Windows PowerShell to partition a Windows To Go device. &nbsp;Hey, Scripting Guy! Am I able to use Windows PowerShell to set up the necessary partitions to prepare a Window To Go key? &mdash;LL &nbsp;Hello LL, Honorary Scripting Guy, Sean Kearney, is here. I&#8217;m continuing with my little chat about setting up Windows To [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73061","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=73061"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73061\/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=73061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}