Resolving Phantom VHDX Attachment in Hyper-V: A Complete Guide
Overview
A common and frustrating issue in Hyper-V is when a virtual machine (VM) gets into a state where a .vhdx
file is believed to be attached, but you can't see or remove it using the GUI. This phantom attachment prevents the VM from booting or re-attaching the disk. This guide walks through identifying, resolving, and preventing this issue.
Symptoms
- VM fails to start.
- Attempting to re-attach a
.vhdx
file results in: The file is already in use
or Access denied
.
- Disk Manager and Hyper-V Manager show inconsistent states.
Root Cause
Hyper-V maintains state information for disk attachments that may not be visible in the GUI. If a VM crashes or is forcibly shut down while using a .vhdx
, the virtual disk file may remain logically attached in the VM configuration—even after reboots.
Step-by-Step Resolution
1. Identify Attached VHDX Files
Get-VMHardDiskDrive -VMName "ubuntu"
2. Forcefully Remove Phantom VHDX
Try the exact location first:
Remove-VMHardDiskDrive -VMName "ubuntu" -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0
Or search and remove:
Get-VMHardDiskDrive -VMName "ubuntu" | Where-Object { $_.Path -like "*efolder.vhdx" } | Remove-VMHardDiskDrive
3. Re-Attach Cleanly
Add-VMHardDiskDrive -VMName "ubuntu" -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0 -Path "D:\disks\efolder\efolder.vhdx"
4. Start the VM
Start-VM -Name "ubuntu"
Optional: Check for File Locks
Use SysInternals:
handle.exe "efolder.vhdx"
Or from PowerShell:
Get-Process | Where-Object { ($_ | Get-ProcessHandle -ErrorAction SilentlyContinue) -match "efolder.vhdx" }
Prevention Tips
- Avoid force shutdowns of VMs with active disks.
- Regularly snapshot/export VM configurations.
- Use fixed VHDX disks when stability is critical.
Mermaid Diagram: Resolution Flow
flowchart TD
A[VM fails to start] --> B[Check disk attachment status]
B --> C{VHDX shown as attached?}
C -- Yes --> D[Run Remove-VMHardDiskDrive]
C -- No --> E[Run Get-VMHardDiskDrive & search by path]
D & E --> F[Run Remove-VMHardDiskDrive on match]
F --> G[Re-attach disk cleanly]
G --> H[Start VM]
H --> I{VM boots?}
I -- Yes --> J[Done âś…]
I -- No --> K[Check file locks or logs]
Visual flowchart of the resolution process.
Conclusion
Phantom .vhdx
attachments are a known but solvable problem in Hyper-V. With PowerShell, you can identify and surgically remove hidden disk associations, restoring full control over your VM configuration.
For deeper issues, you may also consider exporting and re-importing the VM after cleanup.
Need help automating this in a script? Let me know!