Automate Creating and Updating SCCM WinPE Image With Powershell

Many of us who work with SCCM still need to create and update boot images with the latest drivers and maybe add some scripts and tools to the root of the boot image to use before starting the task sequence. Well, a couple of years ago at my job, we had to update the boot image frequently, so I had to automate the process so that everyone on the team could do it. Instead of sitting down and following a guide with DISM and spending a solid two hours, you can just click F5 and voilà, your image is created.

In my case, we also needed the Active Directory PowerShell module installed in the WinPE environment because we needed to run some commands and get some information from the Active Directory. So, you need to have the Remote Server Administration Tools for Windows 10 (RSAT) installed.

To get this working, you will need a couple of things in place before your script works. Let’s jump into the prerequisites:

  • Windows PE add-on for Windows Assessment and Deployment Kit (Windows ADK) – you can download it from here
  • Remote Server Administration Tools for Windows 10 – you can download it from here
  • Folder with the following Structure (you can change the path but you need to update the script with the new path):

Now, you need to copy your WinPE drivers to the Drivers folder and any scripts you would like to have in your WinPE to the Scripts folder. In my case, I used David Dawson’s script to add the Active Directory PowerShell module to WinPE, so you need to have it in your Scripts folder.

Now it’s time for magic! Just start your PowerShell ISE (I use Visual Studio Code) as an administrator and run the script:

# Check for elevation
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    [Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    Write-Warning "Please start the PowerShell prompt as an Administrator and re-run the script."
    Write-Warning "Aborting script..."
    Break
}

#------------------------------------------------------------------------------------------

# Settings variables
$WinPE_BuildFolder = "C:\WinPE\WinPE_x64"
$WinPE_Architecture = "amd64"
$WinPE_MountFolder = "C:\WinPE\Mount"
$WinPE_Drivers = "C:\WinPE\Files\Drivers"
$winPE_Scripts = "C:\WinPE\Files\Scripts"

$ADK_Path = "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit"
$WinPE_ADK_Path = $ADK_Path + "\Windows Preinstallation Environment"
$WinPE_OCs_Path = $WinPE_ADK_Path + "\$WinPE_Architecture\WinPE_OCs"
$Sv_WinPE_OCs_Path = $WinPE_OCs_Path + "\sv-se"
$DISM_Path = $ADK_Path + "\Deployment Tools" + "\$WinPE_Architecture\DISM"
$OSCDIMG_Path = $ADK_Path + "\Deployment Tools" + "\$WinPE_Architecture\Oscdimg"

# Packages To install
$WinPEPackage = @(
"WinPE-HTA.cab",
"WinPE-MDAC.cab",
"WinPE-NetFx.cab",
"WinPE-PowerShell.cab",
"WinPE-WMI.cab",
"WinPE-Scripting.cab",
"WinPE-WDS-Tools.cab",
"WinPE-SecureStartup.cab",
"WinPE-Dot3Svc.cab"
"WinPE-StorageWMI.cab"
)

$WinPEPackage_sv =  @(
"WinPE-HTA_sv-se.cab",
"WinPE-MDAC_sv-se.cab",
"WinPE-NetFx_sv-se.cab",
"WinPE-PowerShell_sv-se.cab",
"WinPE-WMI_sv-se.cab",
"WinPE-Scripting_sv-se.cab",
"WinPE-WDS-Tools_sv-se.cab",
"WinPE-SecureStartup_sv-se.cab",
"WinPE-Dot3Svc_sv-se.cab"
"WinPE-StorageWMI_sv-se.cab"
)

#------------------------------------------------------------------------------------------

# Delete existing WinPE folder (if exist)
try 
{
if (Test-Path -path $WinPE_BuildFolder) {Remove-Item -Path $WinPE_BuildFolder -Recurse -ErrorAction Stop}
}
catch
{
    Write-Warning "Error..."
    Write-Warning "An existing WIM still mounted, use DISM /Cleanup-Wim to clean up and run script again"
    Break
}
# Check for existing folder
if (Test-Path -path "$WinPE_BuildFolder") { Write-Warning "Folder exist, delete it"; Break}

# Copy WinPE boot image & WinPE image files.
Write-Host "Copying WinPE Files...." -ForegroundColor Green
if ( -Not (Test-Path -path "$WinPE_BuildFolder\Sources")) {New-Item "$WinPE_BuildFolder\Sources" -Type Directory | Out-Null} 
Copy-Item "$WinPE_ADK_Path\$WinPE_Architecture\en-us\winpe.wim" "$WinPE_BuildFolder\Sources\boot.wim"
Copy-Item "$WinPE_ADK_Path\$WinPE_Architecture\Media\*" "$WinPE_BuildFolder" -Recurse

# Mount the WinPE image
Write-Host "Mounting WinPE image..." -ForegroundColor Green
$WimFile = "$WinPE_BuildFolder\Sources\boot.wim"
Mount-WindowsImage -ImagePath $WimFile -Path $WinPE_MountFolder -Index 1 | Out-Null

#Add drivers
Write-Host "Adding drivers..." -ForegroundColor Green
Add-WindowsDriver –Path $WinPE_MountFolder -Driver $WinPE_Drivers -Recurse | Out-Null

#Add Packages
Write-Host "Adding WinPE Packages..." -ForegroundColor Green
 
Foreach ($Package in $WinPEPackage){
Write-Host "Importing $Package" -ForegroundColor Green
Add-WindowsPackage –Path $WinPE_MountFolder -PackagePath $WinPE_OCs_Path\$Package | Out-Null
}

Foreach ($Package in $WinPEPackage_sv){
Write-Host "Importing $Package" -ForegroundColor Green
Add-WindowsPackage –Path $WinPE_MountFolder -PackagePath $Sv_WinPE_OCs_Path\$Package | Out-Null
}

#Add DaRT to the boot image
Write-Host "Importing DaRT to boot image..." -ForegroundColor Green
expand.exe "C:\WinPE\Files\Dart\Toolsx64.cab" -F:*.* $WinPE_MountFolder | Out-Null
Copy-Item "C:\WinPE\Files\Dart\DartConfig8.dat" $WinPE_MountFolder\Windows\System32\DartConfig.dat

#Set locale
Write-Host "Setting locale..." -ForegroundColor Green
Dism /Image:$WinPE_MountFolder /Set-SysLocale:sv-SE
Dism /Image:$WinPE_MountFolder /Set-UserLocale:sv-SE
Dism /Image:$WinPE_MountFolder /Set-InputLocale:sv-SE
Dism /image:$WinPE_MountFolder /Set-TimeZone:"W. Europe Standard Time"

#Add CMtrace 
Copy-Item "C:\WinPE\Files\cmtrace.exe" $WinPE_MountFolder\Windows\System32\cmtrace.exe


#Add AD PowerShell module
Set-Location -Path $winPE_Scripts
.\AddADPowerShellModule.ps1 -sourceWinDir $ENV:windir -destinationWinDir $WinPE_MountFolder | Out-Null

Dismount-WindowsImage -Path $WinPE_MountFolder -Save

You can also get the code from my Github

I’m based in Sweden, so you might notice that I added the Swedish versions of the packages and set the locale to Swedish. This is optional and you can comment it out if you like. I also added the cmtrace.exe file so I can use it to read the error logs in the WinPE environment.

I hope you find this helpful. Please comment if you face any issues.

winpe adding powershell support sccm
sccm winpe boot image
updating winpe image
update boot image with storagedrivers
add powershell module to winpe
symenu winpe script
winpe scripting
carsdetail.psml?script_root=
winpe set-syslocale
sccm change winpe image
winpe start powershell
update winpe image msu
winpe autostart sccm
inject powershell module in windows pe
using sccm to create an updated windows pe image with drivers
cant inject winpe adk into boot image
sccm winpe cannot run powershell
sccm copy tools to winpe
creating winpe with powershell
dowenload win pe for sccm
powershell to get drivers added to sccm boot media
does sccm boot media update powershell in winpe?
remote powershell to winpe
how to update the winpe drivers on sccm?
new boot image sccm powrshell
winpe import-module activedirectory
transplants wds module to winpe
sccm winpe powershell startup hook
ardetail.psml?script_root=
powershell in winpe prerequesites
sccm para que se necesita el winpe
update winpe image with cab update
how to update windows pe in sccm
sccm create boot image
how to create a custom winpe image to run customized powershell script
how to inject winpe driver package to sccm boot image
how to connect activedirectory from winpe
enable powershell scripts on boot image sccm
sccm new boot image powershell
upgrade winpe boot image on sccm server
add x86 win pe to sccm
how to run a script as an argument using winpe
peimage upgrade
what is winpe in sccm
create an sccm winpe boot disk
winpe boot image create using powershell
powershell copy sccm boot image
winpe update powershell
adk.jsp?upgrade=
requirements to your sccm winpe boot image to support powershell.
inject files sccm boot image
powershell script to access active directory from winpe
adk.jsp?upgrade_id=
custom boot logs winpe
active directory from winpe
copy active directory module for windows powershell to winpe
script win pe
winpe powershell modules
powershell pe
powershell get wms image in winpe
powershell script to create .wim driver packages
sccm image automation
download latest winpe image for sccm
ad powershell modules in winpe
how to update winpe in sccm
run powershell ise in windows pe
winpe-mdac
powershell update boot image sccm
custom sccm boot image wenplesh.ini asset tag
sccm winpe package path
how to update winpe in sccm?
how to add custom script to execute upon winpe boot

Leave a Comment

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