Use PowerShell to Create Windows To Go Keys—Part 2
Summary: Use Windows PowerShell to partition a Windows To Go device.
Hey, Scripting Guy! Am I able to use Windows PowerShell to set up the necessary partitions to prepare a Window To Go key?
Honorary Scripting Guy, Sean Kearney, is here. I’m continuing with my little chat about setting up Windows To Go keys through Windows PowerShell.
Note This is a five-part series that includes the following posts:
- Use PowerShell to Create Windows To Go Keys—Part 1
Use Windows PowerShell to identify Windows To Go devices.
- Use PowerShell to Create Windows To Go Keys—Part 2
Use Windows PowerShell to partition a Windows To Go device.
- Use PowerShell to Create Windows To Go Keys—Part 3
Use Windows PowerShell to apply an image to a Windows To Go key and make it bootable.
- Use PowerShell to Create Windows To Go Keys—Part 4
Use Windows PowerShell to inject drivers and populate the data for Unattend.xml.
- Use PowerShell to Create Windows To Go Keys—Part 5
Use a basic Windows PowerShell workflow to create multiple devices.
Yesterday, I talked about how to use the Get-Disk cmdlet to identify keys. Today I’m going to use that information to prepare the necessary partition structure.
The first thing I’m going to do is erase everything that is on the key and purge its previous partitions.
For this, I can use the Clear-Disk cmdlet to erase the key. For this cmdlet to work, it needs something specific to target. I could target the friendly name, but I’m going to be very specific and target the disk number, which is unique to the device I want to format.
Yesterday I captured the information from the Get-Disk cmdlet to $WTG:
Now I’ll use the Clear-Disk cmdlet in the following manner to purge the data on this key. Normally, I would be prompted by PowerShell, so I’ll bypass this by setting the Confirm switch to $False:
Clear-Disk –Number $DiskNumber –RemoveData –RemoveOEM –Confirm:$False
For some reason, I have occasionally encountered an issue where Clear-Disk can’t seem to remove the partitions. So I can do some due diligence and use Get-Partition and Remove-Partition first:
Get-Disk –number $DiskNumber | Get-Partition | Remove-Partition –confirm:$False
There! Much cleaner now.
Now I use the Initialize-Disk cmdlet to set up this device for partitions:
Initialize-Disk –Number $DiskNumber –PartitionStyle MBR
I can now start preparing two partitions: the 350 Megabyte System Partition and whatever remains for the operating system.
I’ll use the New-Partition cmdlet to create these. Because I’ll need to reference them afterwards for formatting, I’ll store the objects in PowerShell variables:
$System=New-Partition -DiskNumber $DiskNumber -size (350MB) –IsActive
$OS= New-Partition –DiskNumber $DiskNumber –UseMaximumSize
Now I can format the two partitions by using the Format-Partition cmdlet. I need to supply the objects I previously captured when I created the partitions:
Format-Volume -NewFileSystemLabel “System” -FileSystem FAT32 -Partition $System -confirm:$False
Format-Volume -NewFileSystemLabel “Windows” -FileSystem NTFS -Partition $OS -confirm:$False
I assign each of the drives a letter so I can interact with them later. I’ll use Y for the system drive and Z for the operating system drive. (You can adjust the script to meet your needs.)
Set-Partition -InputObject $System -NewDriveLetter $DriveSystem
Set-Partition -InputObject $OS -NewDriveLetter $DriveOS
One of the most critical pieces is setting the Windows To Go operating system so that if it’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.
Set-Partition -InputObject $OS –NoDefaultDriveLetter
At this point, I have the key ready for my next step, which is to lay down a Windows image. I’ll be getting that done tomorrow.
I invite you to follow The Scripting Guys on Twitter and Facebook. If you have any questions, send an email to The Scripting Guys at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, remember to eat your cmdlets every day with a dash of creativity.
Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy