How to piggyback off an existing WinPE image to implement a custom script

Here’s a trick I used to make my Macrium Reflect boot USB auto-map network drives so that I’d no longer have to manually map them every time I booted it up, but this trick will work for other WinPE setups too, I presume.

Firstly, you’ll need the Windows 10 ADK installed
Secondly, you’ll need your WinPE boot.wim handy. Either with your current bootable USB plugged in, or your source boot.wim that you use to make your WinPE
Thirdly, you’ll need a mounting directory created on your C drive. We’ll be using C:\mnt\. If you don’t have it, create it

With that set up, you’ll want to open “Deployment and Imaging Tools Environment” as ADMIN from your start menu or whatever.
Enter the following, adjusting for your boot.wim and mount locations:
Dism /Mount-Image /ImageFile:"C:\WinPE\media\sources\boot.wim" /index:1 /MountDir:"C:\mnt\mount"

This will mount the contents of your boot.wim into the specified location. In our case, C:\mnt\.
Running your favourite text editor as ADMIN, open up C:\mnt\Windows\System32\Winpeshl.ini.
If that file does not exist, great, that made things easy. If it does, then heck, hold on.

The contents of the Winpeshl.ini dictates what WinPE will launch on startup. You’ll want to take note of what it launches, as you’ll need to implement that into your script later, if you want them to be launched that is.

Change the contents of your Winpeshl.ini to the following:
[LaunchApp]
AppPath = %SYSTEMROOT%\system32\startnet.cmd

Save winpeshl.ini.

With the same text editor, running as ADMIN, open up C:\mnt\Windows\System32\startnet.cmd.

In here, you’ll write your batch script that will do what it is you want it to do. You’ll also want to configure it to START the applications that were specified in your old Winpeshl.ini (if you had one).

Here is my script that I made for my Macrium Reflect network mapping on start-up:

@echo off
wpeinit
echo Starting Macrium...
START "" "%SYSTEMDRIVE%\Program Files\Macrium\launch.exe"

:loop
ping localhost -n 2
net use I: \\sandbox\images /user:domain\username password || goto :loop
net use R: \\sandbox\drivers /user:domain\username password

echo CLOSING THIS WINDOW WILL REBOOT THE MACHINE
pause

Note: If you close the CMD window that appears on boot, it’ll reboot the system, so uh… leave it open in the background or something.

After you’ve made all your changes, you’ll want to unmount and commit the changes made. So close out of your text editor, and make sure your file explorer isn’t buried deep into the file structure of your mount directory, as this will freak out the unmount process.

Going back to your “Deployment and Imaging Tools Environment” window, use the following command, once again adjusting for your mount directory:
Dism /unmount-image /mountdir:"c:\mnt" /commit

Once that completes, your boot.wim will be updated and you can now give it a go booting your winpe drive, fingers crossed you didn’t mess up your script. If you did, plug your USB back into your PC, remount the wim, and tinker with the files again, unmount /commit, safely remove, and retry again. Rinse repeat til you get it right.

Good luck!

Recommended further reading: https://jacobsalmela.com/2016/10/03/customize-winpe-with-wallpaper-and-custom-startup-script/

Leave a Reply

Your email address will not be published. Required fields are marked *