[Parameter(Mandatory=$false)] [string]$BackupPath = "$env:TEMP\vhdx_backup_$(Get-Date -Format 'yyyyMMdd_HHmmss').vhdx" ) if (-not (Get-Module -ListAvailable -Name Hyper-V)) Write-Error "Hyper-V PowerShell module is required." exit 1
catch Write-Warning "Repair-VHD failed: $($_.Exception.Message)" Write-Host "Attempting mount + chkdsk recovery..." $mountResult = Mount-VHD -Path $VhdPath -ReadOnly -PassThru -ErrorAction SilentlyContinue if ($mountResult) $disk = Get-Disk -Number $mountResult.Number -ErrorAction SilentlyContinue if ($disk) Get-Partition repair vhdx powershell
$fixedVHD = Repair-VHD -Path "E:\Corrupt\data.vhdx" -Path "F:\Backups\data_clean.vhdx" -Passthru $fixedVHD | Format-List * repair vhdx powershell
Write-Warning "Automatic repair incomplete. Manual inspection required." | Error | Cause | PowerShell Fix | |-------|-------|----------------| | The VHDX is corrupt | Header or metadata damage | Use Repair-VHD with healthy copy | | The system cannot find the file specified | Path issue or missing parent (differencing) | Provide full absolute path | | Access denied | Permission or locked file | Run as Administrator; ensure no VM attached | | The parameter is incorrect | Fixed-size VHDX passed to Repair-VHD | Convert to dynamic first: Convert-VHD -Path file.vhdx -Destination dynamic.vhdx -VHDType Dynamic | | The VHDX chain is broken | Missing parent in differencing chain | Use Get-VHD -Path child.vhdx -ParentPath to locate parent; restore parent | Checking VHDX Health Without Repair # Health check function function Test-VHDXHealth param([string]$Path) try $vhd = Get-VHD -Path $Path -ErrorAction Stop switch ($vhd.State) "OK" Write-Host "Healthy" -ForegroundColor Green "Corrupted" Write-Host "Corrupt - Needs repair" -ForegroundColor Red "Mounted" Write-Host "Mounted & OK" -ForegroundColor Cyan default Write-Host "Unknown: $($vhd.State)" -ForegroundColor Yellow $vhd catch Write-Error "Cannot read VHDX: $_" repair vhdx powershell
Import-Module Hyper-V -Force Copy-Item -Path $VhdPath -Destination $BackupPath -Force Write-Host "Original VHDX backed up to $BackupPath" Step 2: Try Repair-VHD try Write-Host "Attempting Repair-VHD..." $result = Repair-VHD -Path $VhdPath -Path $HealthyCopyPath -Passthru -ErrorAction Stop Write-Host "Repair-VHD succeeded. State: $($result.State)" exit 0
Optimize-VHD -Path "C:\VHDX\suspect.vhdx" -Mode Full If a differencing VHDX is corrupt and its parent is healthy: