FunctionsToExport/Function_New-WindowsImageVHD.ps1

Function New-WindowsImageVHD {

    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipeline)]
        [ValidateScript({
                $Item = $_ | Get-Item -ErrorAction SilentlyContinue
                if (-not $Item) { throw 'Item ("{0}") does not exist' -f $_ }
                elseif ($Item.Attributes -notmatch "Directory") { throw 'Item ("{0}") is not a directory' -f $_ }
                $true
            }
        )]
        [string]$Location = $PWD,        
        [string]$Name = "WindowsImage",        
        [Parameter(Mandatory)][string]$SourcePath,
        [string]$Edition = "1"
    )

    # Create Directories
    $Directories = @($Location)
    $Directories | ForEach-Object { if (-not (Test-Path $_ -ErrorAction SilentlyContinue)) { New-Item $_ -ItemType Directory -Force } }

    $PSDefaultParameterValues['*-VHDChain:Location'] = $Location
    $PSDefaultParameterValues['*-VHDChain:Name'] = $Name

    # Create WindowsImage
    $Splat = @{
        VHDPath             = Join-Path $Location ('{0}.vhdchain.{1}.vhdx' -f $Name, 0)
        Edition             = $Edition
        SourcePath          = $SourcePath
        DiskLayout          = 'UEFI'
        SizeBytes           = 32TB
        TempDirectory       = Join-Path $env:TEMP 'Convert-WindowsImage'
        RemoteDesktopEnable = $false
        ExpandOnNativeBoot  = $true            
        Passthru            = $true
    }    
        
    Convert-WindowsImage @Splat

    $VHD = Get-VHD $Splat.VHDPath -ErrorAction SilentlyContinue
    if ($VHD) {
        Set-VHDChain -Chain @($VHD)
    }

}