December 4th, 2015

Build a PowerShell-Enabled Windows PE Key: Part 5

Doctor Scripto
Scripter

Summary: Populate a bootable USB key with content from Windows PE.

Honorary Scripting Guy, Sean Kearney, is here today sharing all you need to know about having a Windows PE key with Windows PowerShell.

  Note   This is a five-part series that includes the following posts:

Let’s remember that we’ve got that bootable USB key—a complete structure from Windows PE—sitting on our hard drive…a full tank of gas!

(Oops! Sorry! I was watching Blues Brothers last night.)

Our folder structure in question sits in the Media folder under the structure defined by $WinPEtemp. Our USB key is sitting in the variable defined by $Disk. To choose the USB key, we used this pretty cool little script to display and select the key we wanted to make bootable:

$Disk= Get-Disk | Where-Object { $_.BusType –eq ‘USB’ } | Out-Gridview –passthru

Earlier we defined a partition on this USB key. If we need to find the available partitions, we can pipe this to Get-Partition:

$Disk | Get-Partition

Image of command output

There only one partition on a bootable removable USB key, so only one will show up. The property we are interested in is DriveLetter, and we will store it away like this:

$DriveLetter=($Disk | Get-Partition).DriveLetter

True…I’m not very creative with my PowerShell object names.

So with the location of the USB key and our files ready to be copied, we can place the files on our USB key:

$WinPEKey=”$DriveLetter`:”
Copy-Item -Path "$WinPETemp\Media\*" -destination "$WinPeKey\" –Recurse

Now there is only piece remaining. It should boot up naturally, but if you’d like to play it safe, we can ensure the boot code is injected to the USB key:

& "$($env:windir)\system32\bootsect.exe" /nt60 "$WinPEKey`:"

There are some additional bits to be aware of. At this point, we have customized bootable USB key with lots of PowerShell goodies. If you’d like to, you can make a little modules folder and additional applications on the USB key. Since you can now run PowerShell, some modules (depending on their requirements) can also run in this environment. Of course, commands such as Out-GridView will not work.

Additional changes you could make in the customization phase of the WIM file could be:

  • Add additional drivers to the offline image by using the Add-WindowsDriver cmdlet.
  • Add additional lines to the Startnet.cmd file in the System32 folder of the Windows PE WIM file.
  • Customize Startnet.cmd to launch PowerShell.exe after starting the network.

What is really cool so far is that the user experience in Windows PE is very close to standard Windows PowerShell with PSReadline automatically loading.

So go through the processes, create it, and boot it up.

That is all there is to building a customized Windows PE key with Windows PowerShell 5.0. Stick around for next week as we go about building out a Nano Server by using the new DeployImage module.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then always remember that with great PowerShell comes great responsibility.

Sean Kearney, Honorary Scripting Guy, Cloud and Datacenter Management MVP 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.