This script will build a custom WinPE v3.0 (Windows 7 based) environment and generate a bootable ISO.
It gives you the ability to import a custom startup script (Autostart.cmd), custom wallpaper, custom drivers, registry files and custom Program Files to create a WinPE environment that works for you. You no longer have to waste the time trying to remember those pesky WinPE commands. Let the script do it for you!
# Script name: Build_WinPE.ps1# Created on: 2010-01-05# Author: Gregory Strike# URL: //www.gregorystrike.com/2010/05/17/powershell-script-to-build-a-custom-winpe-v3-0-environment/# Purpose: Builds a customized WinPE v3.0 image from scratch.# Special Files:# Autostart.cmd - Place a file called Autostart.cmd in the $Custom_WinPE\Program Files\System\ and it will run automatically on startup.# winpe.bmp - Place a file called winpe.bmp in $Custom_WinPE\Program Files\System\ and it will replace the default wallpaper.$BuildDir="C:\Temp\WinPE";# No Spaces due to Microsoft's CopyPE Limitation$Architecture="x86"# Architecture Target (x86, amd64, ia64)$Language="en-us"$WAIKInstallation="C:\Program Files\Windows AIK";# Path to the WAIK Installation$ISOLabel="IMAGER"# Volume label for the ISO$Custom_WinPE="C:\Documents and Settings\Administrator\Desktop\WinPE_Files";# The folder for your custom files$DriversDir="$Custom_WinPE\Drivers";# Any drivers you wish added to the image (recursive)$ProgramsDir="$Custom_WinPE\Program Files";# Files\Folders added to "X:\Program Files" (recursive)$WindowsDir="$Custom_WinPE\Windows";# Files\Folders added to "X:\Windows" (recursive)$RegistryDIr="$Custom_WinPE\Registry";# Imports .REG files into the registry. (recursive)# WinPE registry hives are mounted to:# HKLM\Software --> HLKM\PE_Sys# HKLM\System --> HLKM\PE_Soft# You will have to adjust your .REG files accordinglyCLS;#Ensure WAIK PETools are in the Path#This does the same as "Windows AIK\Tools\PETools\pesetenv.cmd"Write-Host("Checking and updating path if needed...")-Fore'Yellow'$Path="";$Path+=$WAIKInstallation+"\Tools\PETools"+";"if($Architecture-ne"x86"){$Path+=$WAIKInstallation+"\Tools\x86;"}if($Architecture-ne"x86"){$Path+=$WAIKInstallation+"\Tools\x86\Servicing;"}$Path+=$WAIKInstallation+"\Tools\"+$Architecture+";"$Path+=$WAIKInstallation+"\Tools\"+$Architecture+"\Servicing;"If(!$Env:Path.ToLower().Contains($Path.ToLower())){[System.Environment]::SetEnvironmentVariable("PATH",$Path+$Env:Path,"process")}#Does BuildPath already exist? If so, Exit.if(Test-Path-path$BuildDir){Write-Host("BuildDir: "+$BuildDir+" already exists. The script will now exit.")-Fore'Red'Exit}$BootWIM=$BuildDir+"\ISO\sources\boot.wim"$Mount=$BuildDir+"\mount"$SystemPath="$Mount\Windows\System32"#System32 is used in all Architecture types for our purposes.#Run Dism CleanupWrite-Host("Running Imagex /Cleanup...")-Fore'Yellow'ImageX/Cleanup#Run CopyPE.cmd to create the initial WinPE ImageWrite-Host("Running CopyPE...")-Fore'Yellow'copype.cmd$Architecture$BuildDir#Moving .WIM image to ISO\sources FolderWrite-Host("Moving WinPE.wim file to ISO\Sources\boot.wim...")-Fore'Yellow'Move-Item"$BuildDir\winpe.wim""$BootWIM"#Mounting ImageWrite-Host("Mounting Boot.wim...")-Fore'Yellow'#Dism /Mount-Wim /WimFile:$BootWIM /Index:1 /MountDir:$MountImageX/MOUNTRW"$BootWIM"1"$Mount"#Adding Packages to imageWrite-Host("Adding Packages...")-Fore'Yellow'Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-WMI.cab"Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-WMI_$Language.cab"Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-Scripting.cab"Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-Scripting_$Language.cab"Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-HTA.cab"Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-HTA_$Language.cab"Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-LegacySetup.cab"Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-LegacySetup_$Language.cab"Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-WMI.cab"Dism/Image:$Mount/Add-Package/PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-WMI_$Language.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-MDAC.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-MDAC_$Language.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-PPPoE.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-PPPoE_$Language.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-Setup.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-Setup_$Language.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-Setup-Client.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-Setup-Client_$Language.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-Setup-Server.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-Setup-Server_$Language.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-SRT.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-SRT_$Language.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\WinPE-WDS-Tools.cab"#Dism /Image:$Mount /Add-Package /PackagePath:"$WAIKInstallation\Tools\PETools\$Architecture\WinPE_FPs\$Language\WinPE-WDS-Tools_$Language.cab"#Adding Drivers to ImageWrite-Host("Installing Drivers...")-Fore'Yellow'Dism/Image:$Mount/Add-Driver/Driver:$DriversDir/Recurse#Copying Program Files FolderWrite-Host("Copying Program Files...")-Fore'Yellow'Copy-Item$ProgramsDir\*"$Mount\Program Files"-Recurse#Copying Windows FolderWrite-Host("Copying to Windows Directory...")-Fore'Yellow'Copy-Item$WindowsDir\*"$Mount\Windows"-Recurse#Importing Custom Registry EntriesWrite-Host("Importing Custom Registry Entries...")-Fore'Yellow'REGLOADHKLM\PE_Sys$Mount\Windows\System32\config\SYSTEMREGLOADHKLM\PE_Soft$Mount\Windows\System32\config\SOFTWAREforeach($fileinGet-ChildItem-Path"$RegistryDir\"-Recurse){Write-Host("...Importing $file")-Fore'Yellow'REGIMPORT$file}REGUNLOADHKLM\PE_SysREGUNLOADHKLM\PE_Soft#If Autostart.cmd exists add it to startup.Write-Host("Checking if $Mount\Program Files\System\Autostart.cmd exists... If so, add it to startup.")-Fore'Yellow'if(Test-Path-path"$Mount\Program Files\System\Autostart.cmd"){Write-Host("Adding Autostart.cmd to startup...")-Fore'Yellow'Add-Content-Path"$SystemPath\startnet.cmd"-Value"""X:\Program Files\System\Autostart.cmd"""}else{Write-Host("Autostart.cmd not found...")-Fore'Red'}#If Winpe.bmp exists replace the default wallpaper.Write-Host("Checking if $Mount\Program Files\System\winpe.bmp exists... If so, replace wallpaper.")-Fore'Yellow'if(Test-Path-path"$Mount\Program Files\System\winpe.bmp"){Write-Host("Updating Wallpaper...")-Fore'Yellow'Move-Item"$Mount\Program Files\System\winpe.bmp""$SystemPath\winpe.bmp"-Force}else{Write-Host("Wallpaper not found...")-Fore'Red'}#Unmount imageWrite-Host("Unmounting\Committing Image...")-Fore'Yellow'#Dism /Unmount-Wim /MountDir:$Mount /CommitImageX/Unmount"$Mount"/Commit#Create ISOWrite-Host("Building ISO...")-Fore'Yellow'$TimeStamp=Get-Date-Formatyyyy-MM-dd;oscdimg"-n""-l$ISOLabel""-b$BuildDir\etfsboot.com""$BuildDir\ISO""$BuildDir\($TimeStamp) WinPE.iso"