9

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?

  • 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 – barlop Aug 30 '15 at 21:19
  • @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. – Zerobinary99 Aug 31 '15 at 0:29
  • 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 – barlop Aug 31 '15 at 1:02
  • @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. – Zerobinary99 Aug 31 '15 at 1:07
  • 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? – barlop Aug 31 '15 at 1:28
6

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:

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
|improve this answer|||||
3

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.

|improve this answer|||||
  • 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." – Zerobinary99 Apr 5 '17 at 7:56
  • 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. – Anton Krouglov Apr 6 '17 at 11:38
  • 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 – Zerobinary99 Apr 7 '17 at 13:03
  • 1
    I have the similar problem.googling have yield your old question. So this answer may be helpful to other people as well. – Anton Krouglov Apr 7 '17 at 13:56
  • 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 – FreeSoftwareServers Jan 27 '18 at 13:09
1

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

|improve this answer|||||
  • 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. – Zerobinary99 Jan 27 '18 at 13:01
  • Aside from using the built in windows function without using 3rd party tools? Seems oximoronic. – FreeSoftwareServers Jan 27 '18 at 13:13
  • I was asking for other native Windows functions. There's nothing oxymoronic about that. Seems to me that you have general comprehension issues. – Zerobinary99 Jan 27 '18 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.