How can I release a locked Hyper-V VHD that is used by another process?
It's also possible for Windows to lock a VHD file. I ran into this by double-clicking on a VHD, which causes recent version of Windows to open it up like a folder. However, it appears that the system then holds a lock on the file, which prevents Hyper-V from using it to start a VM. Rebooting fixed that problem.
While some of the simple answers provided above may work for some users, none of them solved my issue. My problem was that the Hyper-V .vhdx file for my virtual machine was locked but not appearing in either File Explorer as an ejectable disk nor in Administrative Tools Disk Management component. Note that my VM is a Debian Linux derivative running under WSL2 in Windows 10 with Hyper-V enabled.
So how did it become locked in the first place? I followed the instructions from an article on the web that said I can easily view files by simply double-clicking the .vhdx file. With the virtual machine shut down for the moment I did just that but got an error message (something about "can't read file"). Afterwards, trying to connect to my virtual machine with Hyper-V resulted in the error message "the process cannot access the file because it is being used by another process". So how to unlock it?
After scouring the web and trying various procedures I finally found one that worked by using the Powershell. Here's the short procedure:
-
Search for Powershell and then Run as Administrator.
-
At the command prompt type:
get-physicaldisk
In my case the results were:
Number FriendlyName SerialNumber MediaType CanPool OperationalStatus HealthStatus Usage Size
------ ------------ ------------ --------- ------- ----------------- ------------ ----- ----
1 SanDisk Ultra II 480GB 16xxxxxxxx96 SSD False OK Healthy Auto-Select 447.13 GB
0 WDC WD1001FALS-00E3A0 WD-WCATxxxxxxx9 Unspecified True OK Healthy Auto-Select 931.51 GB
2 Msft Virtual Disk SSD False OK Healthy Auto-Select 32 GB
As you can see there is an entry called "Msft Virtual Disk" which represents the VM file mounted as a virtual disk.
- To unmount the virtual disk, from the command prompt type:
Dismount-DiskImage -ImagePath "C:\Users\Public\Documents\Hyper-V\Virtual hard disks\Name-Of-Your-Virtual-Machine.vhdx"
- Re-enter the command:
get-physicaldisk
to verify that the virtual disk is unmounted.
After that the virtual disk was unmounted and I was able to successfully connect and load my virtual machine from Hyper-V once again.
I hope this information helps someone!