Skip to content

MDT2013 Automated WIM import

As creating a new wim with a new Windows version is pretty time consuming, I’ve created a script that does the work for you.
All you need is the new ISO and select the edition you want.

# Load MDT snappin.
Add-PSSnapIn Microsoft.BDD.PSSnapIn

# Set error log location.
$ErrLogLocation = "C:\temp\Error.html" 

# Import DISM modules from Windows 10 ADK if version is not correct
#$env:Path = "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\DISM"
#Import-Module "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\DISM"

# Create C:\WIM to store the install.wim.

New-Item -Path ($env:HOMEDRIVE + "\") -Name WIM -ItemType Directory | Out-Null

# Select your ISO file to import.

Write-Host "Select the ISO you want to import." -BackgroundColor Green -ForegroundColor Black
$ISO = New-Object System.Windows.Forms.OpenFileDialog
$ISO.InitialDirectory = $env:HOMEDRIVE
$ISO.MultiSelect = $false
$ISO.ShowDialog()
 
$CheckExt = [IO.Path]::GetExtension($ISO.FileName)

 if ($CheckExt -eq '.iso'){
  Write-Host "Imported file: $($ISO.SafeFileName)" -BackgroundColor Green -ForegroundColor Black
  }else{
  Write-Host "The imported file is not a .ISO, try again..." -ForegroundColor Red
  }

$ISOImagePath = Mount-DiskImage -PassThru $ISO.FileName
$GetLetter = Get-Volume -DiskImage $ISOImagePath

Write-Host "Copying $(($GetLetter.DriveLetter + ":\sources\install.wim")) to $(($env:HOMEDRIVE + "\WIM"))..." -BackgroundColor Green -ForegroundColor Black
Copy-Item -Path ($GetLetter.DriveLetter + ":\sources\install.wim") -Destination ($env:HOMEDRIVE + "\WIM")

# Set location to same as WIM file folder, create emtpy/temp folder.

$Loc = ($env:HOMEDRIVE + "\WIM")
Set-Location -Path $Loc
New-Item -Name Empty -ItemType Directory | Out-Null
New-Item -Name Temp -ItemType Directory | Out-Null

# Select the Windows image you want to extract.

$SelectedImage = @()
$SelectedImage += Get-WindowsImage -ImagePath "$Loc\install.wim" | Out-GridView -PassThru

Foreach ($Selected in $SelectedImage) {

Write-Host "You have chosen for $($Selected.ImageName)" -BackgroundColor Green -ForegroundColor Black

# Create capture WIM in \temp.

Write-Host "Creating new WIM in $Loc\Temp\install.wim..."  -BackgroundColor Green -ForegroundColor Black
New-WindowsImage -ImagePath "$Loc\Temp\$($Selected.ImageDescription).wim" -CapturePath "$Loc\Empty" -Name "$($Selected.ImageDescription)" -CompressionType max | Out-Null
$TempWIM = "$Loc\Temp\$($Selected.ImageDescription).wim"

# Export install.wim to empty WIM.

Write-Host "Exporting $Loc\install.wim with $($Selected.ImageName) to $Loc\Temp\$($Selected.ImageName).wim..."  -BackgroundColor Green -ForegroundColor Black

Export-WindowsImage -SourceImagePath "$Loc\install.wim" -SourceIndex $($Selected.ImageIndex) -DestinationImagePath $($TempWIM) -DestinationName "$($Selected.ImageDescription)" -CompressionType max | Out-Null

# Clear ImageIndex where ImageSize -eq 0.

$TempImage = Get-WindowsImage -ImagePath $TempWIM
$ImageSize = $TempImage | Where-Object { $_.ImageSize -eq '0'}
Remove-WindowsImage -ImagePath $TempWIM -Index $ImageSize.ImageIndex -InformationAction SilentlyContinue | Out-Null

# Import the WIM into MDT.

$WinInfo = Get-WindowsImage -ImagePath $TempWIM -Index 1
$WinVer = $($Selected.ImageDescription)

switch -Wildcard ($WinVer)
    {
    "Windows 10*" { $WinVer = "Windows 10" }
    "Windows 8*"  { $WinVer = "Windows 8" }
    "Windows 7*"  { $WinVer = "Windows 7" }
    default { Write-Host "No valid Windows version." -ForegroundColor Red } 
    }
 
# Set PSDrive, create new if unavailable.

$PSDrive = "DS001:"

if (!(Test-Path -Path $PSDrive)){
 Write-Host "Creating new PSDrive..."  -BackgroundColor Green -ForegroundColor Black
 New-PSDrive -Name "DS001" -PSProvider MDTProvider -Root "D:\DeploymentShare" | Out-Null
 }else{
 Write-Host "PSDrive is available."
 }

# Import exported WIM's in to MDT.

Write-Host "Importing $($TempWIM) into MDT..." -BackgroundColor Green -ForegroundColor Black
Import-MDTOperatingSystem -Path "$($PSDrive)\Operating Systems\$($WinVer)" -SourceFile $($TempWIM) -DestinationFolder "$($Selected.ImageDescription) - $($WinInfo.Version.ToString()) - $($WinInfo.Languages)" | Out-Null
}

# Dismount mounted ISO.

Write-Host "Dismounting the ISO ($($ISO.SafeFileName))" -BackgroundColor Green -ForegroundColor Black
Dismount-DiskImage $ISO.FileName

# Remove C:\WIM folder after import.

Write-Host "Removing the folder and contents: $Loc..." -BackgroundColor Green -ForegroundColor Black
Set-Location $env:HOMEPATH
Remove-Item $Loc -Recurse -Force

# Error logging.

Write-Host "Error log available in $ErrLogLocation"
$Error | ConvertTo-Html -As List > $ErrLogLocation
#$Error.Clear()

All that is left is to load the freshly added Operating System into your Task Sequence:


Published inMDT2013Server 2012Server 2016Windows 10Windows Server

Be First to Comment

Leave a Reply

Your email address will not be published.