May 14 2023 03:18 PM - edited May 14 2023 03:18 PM
Here is a slightly modified version of the script https://github.com/TheGabeMan/Hyper-V-MergeAVHDX
The problem with about script is it uses the last written time. this may be changed when we mess with an avhd file.
Modified it to use creation time instead. Thanks TheGabeMan
VHD Merge Script
# Path to AVHDX
$PathToVHDX = ""
$AlleFiles = Get-ChildItem -path $PathToVHDX
$AlleFiles = $AlleFiles |Sort-Object -Property CreationTime
## Filter on avhdx and select the youngest
$LatestAHVDX = $AlleFiles | Where-Object { $_.Name -like "*.avhdx" } | Sort-Object CreationTime -Descending | Select-Object -First 1
While ( $LatestAHVDX.count -gt 0)
{
## ## Filter on avhdx and select the youngest
Write-host "Youngest AVHDX = $($LatestAHVDX.Name) - $($LatestAHVDX.CreationTime)"
# Merge youngest AVHDX into VDX
$Source = $($PathToVHDX + "\" + $($LatestAHVDX.Name))
$VHDSource = get-vhd $Source
write-host "Merge: $source to $($VHDSource.ParentPath)"
Merge-VHD -Path $Source -DestinationPath $VHDSource.ParentPath
$AlleFiles = Get-ChildItem -path $PathToVHDX
$AlleFiles = $AlleFiles | Sort-Object CreationTime
Write-host "Number of files: $($AlleFiles.count)"
## ## Filter on avhdx and select the youngest
$LatestAHVDX = $AlleFiles | Where-Object { $_.Name -like "*.avhdx" } | Sort-Object CreationTime -Descending | Select-Object -First 1
}