How to unmount a VHD via command line in Windows 10

14,633

Solution 1

Since I haven't found a way to solely rely on MS-installed command line tools, I'll list third party solutions.


WinApiExec

WinApiExec enables the user to call the Windows API from the command line. The 32bit executable is only 3.5KB in size (x64: 5.5KB) and therefore ridiculously small, introducing as little overhead as probably possible.

winapiexec.exe virtdisk.dll@OpenVirtualDisk $a:2,0 "C:\Example Path\Test.vhd" 262144 0 0 $b:4 , virtdisk.dll@DetachVirtualDisk $$:7@0 0 0

vMount

vMount supports quite a few VHD-related functions. Its size (x86: 432KB; x64: 2.53MB) is rather huge for merely calling WinApi, so it's not a solution for my scenario, but might be useful to other people.

vmount detach \\.\PhysicalDriveX

DiskPart

For the sake of completeness DiskPart shall be mentioned, too. It supports a great deal of disk-related features, but has a rather sluggish way of going about it to automate the otherwise necessary user input for scripting purposes. It's also the only tool on the list that will be trapped in a loop if you accidentally run the script without elevated rights.

Save the following two lines into a text file:

select vdisk file="C:\Example Path\Test.vhd"
detach vdisk

Now, Dism can parse the text file as a form of unattended answer file:

DISKPART /s C:\Path_to\Name_Of_Textfile.txt

Solution 2

You can mount and unmount vhd/vhdx using native Windows 10 Powershell:

Import-module hyper-v
Dismount-VHD -Path 'c:\my.vhd'

From cmd/bat:

powershell -command "Import-module hyper-v; Dismount-VHD -Path 'c:\my.vhd'"

In either case you have to use elevated (Run as admin) prompt.

Another thing to note is that you need to enable Hyper-V feature first.

Solution 3

This is an expanded answer: (You should be able to glean how to umount VHD)

HowTo Automatically mount and umount VHD files on boot/shutdown.

Things seem to have changed a little in Windows 10.

First, I should say that I am mounting a VHD stored on a Linux File Server, mounting in over SMB.

Second, very important, is that if you don't detach the disk upon reboot, then when you try to re-attach it, it says "The file is in use and can't be accessed"..

So we need both automount and unmount. The unmount proved a bit tricker, since I cant just put it in startup folder. You have to use Group Policy. Also, when run as a "shutdown/startup" script, it ran to early, you need to run it at "Logon/Logoff"

I have 4 files in C:\mount

mount.cmd

@ECHO OFF

DISKPART /s C:\mount\mount.txt

umount.cmd

@ECHO OFF

DISKPART /s C:\mount\umount.txt

umount.txt

select vdisk file="\\192.168.1.255\Blizzard.vhdx"
detach vdisk

mount.txt

select vdisk file="\\192.168.1.255\Blizzard.vhdx"
attach vdisk

Now in start type/run gpedit.msc and add the scripts to logon/logoff

enter image description here

Share:
14,633

Related videos on Youtube

Zerobinary99
Author by

Zerobinary99

Updated on September 18, 2022

Comments

  • Zerobinary99
    Zerobinary99 almost 2 years

    I need to unmount a VHD with a batch script in Windows 10. The only solution I found online was directed at Windows 7 and involved Diskpart. Are there any other native Windows command line tools available that can do the job?

    • barlop
      barlop almost 9 years
      in cmd may be tricky. I don't know powershell but powershell does so much.. e.g. many links say powershell can do what you want virtualizationadmin.com/kbase/VirtualizationTips/… and technet.microsoft.com/en-us/library/hh848562(v=wps.630).aspx
    • Zerobinary99
      Zerobinary99 almost 9 years
      @barlop Thank you for your comment and the links. The batch needs to be able to run on a system where Powershell isn't installed, so I'm afraid that's not an option.
    • barlop
      barlop almost 9 years
      is there any particular reason why you don't want to use diskpart? e.g. it can run in a non-interactive way, so you could have a batch file that echos out a diskpart script into a file script.txt, then does diskpart /s script.txt there's an example of a diskpart script here technet.microsoft.com/en-us/library/dn614984.aspx
    • Zerobinary99
      Zerobinary99 almost 9 years
      @barlop I'm aware of the diskpart-solution and I'm currently using it on a different system. The one where this is intended to work on has no diskpart on it.
    • barlop
      barlop almost 9 years
      I think it'd be hard enough even finding a 3rd party one that does. Native commands are pretty well known, i'm sure there aren't any that do it other than diskpart. It's almost a miracle that diskpart does it. (since diskpart is such a "simple" program rather like fdisk was, and people tend to use it without noticing the option. The idea that there's another native command that'd do it, is almost unthinkable. If you can put a batch file on there, can you not put a program like diskpart on there?
    • Zerobinary99
      Zerobinary99 almost 9 years
      Why would it be unthinkable? It's a very simple program to write and there are plenty of solutions available such as "Dismount-VHD" and "Dismount-DiskImage" which are both command line tools of Microsoft's server line up. Vmount is a freeware third-party solution, but it's a huge 2.5MB executable which is also buggy and doesn't properly work on Windows 10. Diskpart supports formating disks, yet Microsoft also delivered format.exe to do the job without diskpart. So why wouldn't there be tools for other functions such as vhd-handling? The Server editions indicate that there might be some.
    • barlop
      barlop almost 9 years
    • Zerobinary99
      Zerobinary99 almost 9 years
      There will be no discussion. The outrageous claims you made in your last reply were clearly not based on knowledge, but ignorance about the subject. I appreciate your effort, but if you're not capable to give an answer then you shouldn't be hell-bent on trying to push your solutions on me. I have my reasons for the requirements stated.
    • barlop
      barlop almost 9 years
      Well, keep trying to find native cmd.exe ways of mounting a vhd, besides diskpart. And we shall see if you succeed
    • Zerobinary99
      Zerobinary99 almost 9 years
      @barlop The question is about unmounting not mounting images. Mounting is dead easy by just calling the name of the VHD in the batch-file. No tools required...
  • Zerobinary99
    Zerobinary99 about 7 years
    From my comment to my question: "The batch needs to be able to run on a system where Powershell isn't installed, so I'm afraid that's not an option."
  • Anton Krouglov
    Anton Krouglov about 7 years
    As far as I am aware Powershell is an integral part of Windows 10 (you have asked about Windows 10), so you cannot install or uninstall it. However its execution may be blocked by various means, like Group Policy.
  • Zerobinary99
    Zerobinary99 about 7 years
    Why are you questioning the conditions? There's a reason for them. I'm running a Windows Lite without Powershell and I'm averse to keeping an entire platform that I don't need if there are more light weight options available
  • Anton Krouglov
    Anton Krouglov about 7 years
    I have the similar problem.googling have yield your old question. So this answer may be helpful to other people as well.
  • Zerobinary99
    Zerobinary99 over 6 years
    Thanks for the answer, but this is not a solution to the problem. I was looking for a batch-compatible solution aside from using Diskpart. You basically just pasted the same syntax that's already in my answer.
  • FreeSoftwareServers
    FreeSoftwareServers over 6 years
    Since this question is over two years old and the title is vague regarding your requirement, this answer helps many other people as does my expanded answer. You should read more stack exchange Meta to learn more about how this site operates and its policies. These kind of answers are very useful for the community @Zerobinary99
  • Zerobinary99
    Zerobinary99 over 6 years
    I was asking for other native Windows functions. There's nothing oxymoronic about that. Seems to me that you have general comprehension issues.
  • Zerobinary99
    Zerobinary99 over 6 years
    There's nothing vague about the requirement. Learn to read and refrain from answering if you don't understand the question. I suggest you read more about the site's policies. It's not about how useful answers are to the general public, but whether they fit to the question in the first place and yours doesn't. I agree that this answer does have some value for those having access to Powershell, though.
  • FreeSoftwareServers
    FreeSoftwareServers over 3 years
    @Zerobinary99 fair enuf, but this will help others who end up here.