FunctionsToExport/Function_Add-VHDChain.ps1

Function Add-VHDChain {

    [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,

        [ValidateNotNullOrEmpty()]
        [string]$Name = "VHDChain",   

        [switch]$Passthru

    )

    $Chain = Get-VHDChain -Name $Name -Location $Location

    $Splat = @{
        Path         = Join-Path $Location ('{0}.vhdchain.{1}.vhdx' -f $Name, ($Chain.Index[-1] + 1))
        ParentPath   = $Chain[-1].Path
        Differencing = $true
    }
    $VHD = New-VHD @Splat

    if ($VHD) {

        $Splat = @{
            Name     = $Name
            Location = $Location
            Chain    = @($Chain) + $VHD
        }
        Set-VHDChain @Splat
    
        if ($Passthru) { $VHD }

    }

}