Hi Everybody

i thought i would post the basics of my powershell script as this may be useful for other people wanting to move to powershell scripted imaging/DISM.

this script removes all partitions on disk 0, converts to GPT (if required), creates new partitions for UEFI, deploys single .wim image file, installs drivers and recreates boot files (retains image safe data).

this has been designed to connect (using ps-drive to map z drive (net use would also work)) to windows server with a file share (i am using a hidden share called imaging$) on either a primary or satellite windows server (the address of the windows server that WinPE has booted from is pulled from the registry in winpe (HKLM:\System\controlset001\control\ PEbootserveraddr)).


the folder containing a systems drivers must be named using the model number from the bios PS: (gwmi Win32_ComputerSystem).model as this is command that will map the path to the drivers during the imaging process.

shared folder on server has the subfolders


drivers -sub folders Windows 10, Windows 7


the images folder should contain a sysprepped .wim file of your Image where you have captured the OS partition only. as were a recreating all boot files you do not need to capture the system partition.

the diskpart folder has a .txt file called "Convert disk0 GPT.txt"

rem---Convert disk0 GPT.txt cmds

select disk 0

convert gpt


rem----Convert disk0 GPT.txt cmds

Third party powershell script

#name of Image & driver share

$imagepath = "imaging$"

#check bios config, must be in UEFI

#check bios state

$bios = get-itempropertyvalue HKLM:\System\CurrentControlSet\Control PEFirmwareType

if($bios -eq "1"){write-host "Bios has booted in MBR mode, this deployment is only intended for machines that are configured to boot in UEFI.
please press enter to shutdown and modify bios accordingly to boot in UEFI" -foregroundcolor red
wpeutil shutdown
if($bios -eq "2"){write-host "Bios has booted in UEFI" -foregroundcolor green
start-sleep -s 05

#get model number from bios

$model = (gwmi Win32_ComputerSystem).model

#start time of imaging work

$StartTime = $(get-date)

#get zenworks server address from WinPE registry

$ZenServer = (Get-ItemProperty -path HKLM:\System\controlset001\control\).PEbootservera ddr

#credentials to map psdrive

$username = "Domain.name\User_account"
$password = convertto-securestring -String "Password" -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

#map drive

Write-Host "$ZenServer\$imagepath" -foregroundcolor green

Try{$psdriveresult = New-PSDrive -Name Z -PSProvider FileSystem -Root "\\$ZenServer\$imagepath" -Credential $Cred -persist -EA Stop }
Catch [System.Runtime.InteropServices.ExternalException]{
return "$_"
Write-Host "$_" -ForegroundColor green
Catch [System.Management.Automation.ParameterBindingExcep tion]{
return $_
Write-Host $_ -ForegroundColor blue -BackgroundColor white
return "OTHER:$_"
Write-Host "OTHER:$_" -ForegroundColor red -BackgroundColor white

write-host "$psdriveresult" -foregroundcolor green

#select image by the most recent file write time, use where object to filter results, sort by creation time and select first one will choose the last modfifed (newest) file

$image = Get-ChildItem -Path Z:\Images |where {$_.extension -eq ".wim"} | Where-Object {$_.Name -match "Windows 10"} | Where-Object {$_.Name -match "1803"} |Sort-Object CreationTime -Descending | Select-Object -First 1

$Imagefile = $image.Name

#set path to drivers, our images have this naming convetion: Win10 Enterprise 1709 June 2018 Admin.wim. the if statement sets the path to the OS driver folder

if($Imagefile.StartsWith("Win7")){$driverpath = "Z:\Drivers\Windows 7"}
if($Imagefile.StartsWith("Win10")){$driverpath = "Z:\Drivers\Windows 10"}

# diskpart. all partitions on disk 0 are removed, if the disk is MBR it's converted to GPT and new partition are created to deploy wim file

#clear partitions
{Get-Partition -DiskNumber 0 -ea SilentlyContinue |Remove-Partition -Confirm:$false -ea SilentlyContinue}

#check if disk is MBR
if((Get-Disk -Number 0).PartitionStyle -eq "GPT"){"Disk 0 is in GPT"}
diskpart.exe /s "Z:\DiskPart\Convert disk0 GPT.txt"

#system partition
New-Partition -DiskNumber 0 -GptType '{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}' -Size 100MB -DriveLetter S |

Format-Volume -FileSystem FAT32 -NewFileSystemLabel System -Confirm:$false

#MSR partition
New-Partition -DiskNumber 0 -GptType '{e3c9e316-0b5c-4db8-817d-f92df00215ae}' -Size 128MB

#Windows Partition.# driver letter W is selected to avoid letter conflicts in winpe. windows assigns C drive mapping automattically during sysprep.
New-Partition -DiskNumber 0 -GptType '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' -UseMaximumSize -DriveLetter W |

Format-Volume -FileSystem NTFS -NewFileSystemLabel OSDisk -ShortFileNameSupport $False -Confirm:$false

# apply image using DISM. we are only applying the windows image, this can be captured from MBR or UEFI.

dism /Apply-Image /ImageFile:"Z:\Images\$Imagefile" /index:1 /ApplyDir:W:\

# install drivers using the model number from the bios DISM will recurse through folder installing all drivers (thse must be in .inf format)

# dism /image:W:\ /add-driver:"$images\drivers\$model\" /recurse

if(test-path "$driverpath\$model"){
dism /image:W:\ /add-driver:"$driverpath\$model" /recurse /ForceUnsigned

#install nvidia drivers
#dism /image:W:\ /add-driver:"z:\drivers\nvidia" /recurse /ForceUnsigned
else{write-host write-host "Drivers are not availible for this machine model number: $model" -foregroundcolor red
wpeutil shutdown


# rebuild bcd #bcdboot W:\Windows

$bootfiles = bcdboot.exe W:\Windows /s s: /f UEFI

write-host $bootfiles -foregroundcolor green

#set system drive as bootmgr
cmd.exe /c "Bcdedit /set {bootmgr} device partition=s:"
cmd.exe /c "Bcdedit /set {memdiag} device partition=s:"

#set defualt path for windows partition
cmd.exe /c "Bcdedit /set {default} device partition=w:"
cmd.exe /c "Bcdedit /set {default} osdevice partition=w:"

#add windows boot into bios menu. to keep PXE as first boot we are using /addlast
cmd.exe /c "Bcdedit /set {FWbootmgr} displayorder {bootmgr} /addlast"

#set just imaged flag
$zis = cmd.exe /c zisedit.exe j=False

write-host $zis -foregroundcolor green

$elapsedTime = $(get-date) - $StartTime

$totalTime = "{0:HH:mm:ss}" -f ([datetime]$elapsedTime.Ticks)

write-host "Image Deployment time HH:mm:ss $totalTime" -foregroundcolor green

Start-Sleep -Seconds "10"

#remove PSdrive

Remove-PSDrive z -Force -ErrorAction SilentlyContinue

exit 0