Skip to content

Instantly share code, notes, and snippets.

@davegreen
Created February 1, 2016 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davegreen/4dc3f3793f2a03a10ff8 to your computer and use it in GitHub Desktop.
Save davegreen/4dc3f3793f2a03a10ff8 to your computer and use it in GitHub Desktop.
This function mounts and customises a given WIM image by removing pre-provisioned Appx packages and slipstreaming updates. Defaults provided for Appx package removal are based around Windows 10.
Function Build-CustomWIMImage
{
[CmdletBinding(SupportsShouldProcess)]
Param(
[parameter(Mandatory=$true, Position=1)][ValidateScript({ Test-Path $_ })][string]$ImagePath,
[parameter()][ValidateScript({ Test-Path $_ })][string]$UpdatesPath,
[parameter()][ValidateNotNullOrEmpty()][string[]]$RemoveAppxPackages = {
'Microsoft.3DBuilder',
'Microsoft.BingFinance',
'Microsoft.BingNews',
'Microsoft.BingSports',
'Microsoft.Getstarted',
'Microsoft.Messaging',
'Microsoft.MicrosoftSolitaireCollection',
'Microsoft.MicrosoftOfficeHub',
'Microsoft.Office.OneNote',
'Microsoft.Office.Sway',
'Microsoft.People',
'Microsoft.SkypeApp',
'Microsoft.WindowsCommunicationsApps',
'Microsoft.WindowsFeedback',
'Microsoft.WindowsPhone',
'Microsoft.XboxApp',
'Microsoft.XboxGameCallableUI',
'Microsoft.XboxIdentityProvider',
'Microsoft.ZuneVideo',
'Microsoft.ZuneMusic',
'Windows.ContactSupport'
}
)
# Folder variables and assorted shortcuts.
$workDir = (Split-Path -Path $ImagePath)
$mntDir = 'imgmnt-' + (Get-Date -Format 'yyyy-MM-dd')
$mntPath = Join-Path -Path $workDir -ChildPath $mntDir
Write-Verbose -Message 'Setup Information : Creating WIM mount directory.'
New-Item -ItemType Directory -Path $workDir -Name $mntDir | Out-Null
try
{
Write-Verbose -Message 'Setup Information : Testing paths before continuing.'
Test-Path $mntPath -ErrorAction Stop | Out-Null
Test-Path $workDir -ErrorAction Stop | Out-Null
Test-Path $mntDir -ErrorAction Stop | Out-Null
}
catch
{
Write-Error -Message "One or more of the following paths does not exist. Please check and retry: `nMount Path: $mntPath `nWork Dir: $workDir `nMount Dir: $mntDir"
exit
}
Write-Verbose -Message 'Setup Information : Changing to working directory.'
Set-Location $workDir
Write-Verbose -Message 'Setup Information : Mounting WIM image.'
if ($PSCmdlet.ShouldProcess($ImagePath, 'Mount-WindowsImage'))
{
try
{
Mount-WindowsImage -CheckIntegrity -Path $mntPath -ImagePath $ImagePath -Index 1
}
catch
{
Write-Error -Message 'Could not mount WIM image successfully. Check the below error and retry'
Write-Error -Exception $error[0]
exit
}
}
Write-Verbose -Message 'Setup Information : Getting AppxProvisionedPackages.'
if ($PSCmdlet.ShouldProcess($mntPath, 'Get-AppxProvisionedPackage'))
{
$ProvisionedPackages = Get-AppxProvisionedPackage -Path $mntPath
}
if ($UpdatesPath)
{
foreach ($update in (Get-ChildItem $UpdatesPath))
{
Write-Verbose -Message "Updating image : Adding $($update.Name)."
if ($PSCmdlet.ShouldProcess($($update.FullName), 'Dism.exe Add-Package'))
{
Dism.exe /Image:$mntPath /Add-Package /PackagePath:$($update.FullName)
}
}
}
foreach ($App in $RemoveAppxPackages)
{
$ProvisionedPackage = $ProvisionedPackages | Where-Object {$_.displayName -eq $App}
if ($ProvisionedPackage)
{
Write-Verbose -Message "Removing Appx Package : $App."
if ($PSCmdlet.ShouldProcess($($ProvisionedPackage.PackageName), 'Remove-AppxProvisionedPackage'))
{
Remove-AppxProvisionedPackage -Path $mntPath -PackageName $ProvisionedPackage.PackageName | Out-Null
Write-Output "Removed : AppxProvisionedPackage $App"
}
}
else
{
Write-Warning -Message "Unable to find package: $App. This image may already have been modified, or this application removed."
}
}
Write-Output 'Packages present in WIM after work:'
Get-AppxProvisionedPackage -Path $mntPath
Write-Verbose -Message 'Configuration : Complete, saving image.'
try
{
if ($PSCmdlet.ShouldProcess($mntPath, 'Dismount-WindowsImage'))
{
Dismount-WindowsImage -Path $mntPath -CheckIntegrity -Save
}
}
catch
{
Write-Warning -Message 'Image could not be dismounted cleanly, see error.'
Write-Error -Exception $error[0]
}
Write-Verbose -Message 'Cleaning up : Image mount directory.'
Remove-Item -Path $mntPath -Force | Out-Null
}
Build-CustomWIMImage -ImagePath "$env:USERPROFILE\Downloads\install.wim" -UpdatesPath "$env:USERPROFILE\Downloads\ImageUpdates\" -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment