Since Windows 7 and Windows Server 2008 R2 users can create and mount virtual hard disk files (VHD/VHDX) directly from Disk Management console. These virtual disks look like separate physical disks in the system and can be used to store any data. An essential disadvantage of these disks is the necessity to mount VHD/VHDX files manually each time you reboot Windows. In addition, only local computer administrators have the permissions to mount VHD/VHDX files, ordinary users don’t have these privileges (those, the user must ask the administrator in order to mount a virtual disk file).
In this article we’ll show how to automatically mount the vhd/vhdx file at the Windows startup so that virtual hard disks become available for non-administrator users.
In Windows there is no built-in ability to automate mounting of vhd files when you reboot your system. Let’s try to implement auto-mount vhd images using the diskpart and Windows Task Scheduler.
So, create a new text file (C:\Scripts\attach_vhdx.txt) containing the following code:
select vdisk file="C:\vhd2\w10vhd.vhdx"
attach vdisk
select part 1
assign letter=K
exit
- C:\vhd2\w10vhd.vhdx – is the full path to the VHD or VHDX disk image;
- K – the drive letter that you want to assign to your VHD image.
This script mounts the specified vhdx file, selects the first partition on it and assigns drive letter. If there are several partitions on the disk, change the code accordingly by specifying the number of the partition to be mounted.
Run Windows Task Scheduler (Taskschd.msc) and create a new Scheduler task (Actions -> Create Task). Configure it as follows:
General tab:
- Specify the name of the task (Name): automountvhdx;
- Check Run whether user is logged on or not;
- Check Run with highest privileges.
Triggers tab:
- Add a new startup trigger (At Startup) with the default settings.
Actions tab:
- Create a new action (New);
- The action is Start a program;
- Program/script: diskpart;
- Specify as a program argument: /s “C:\Scripts\attach_vhdx.txt”.
In the Conditions tab, uncheck Start the task only if the computer is on AC power.
Save the changes. The system will prompt you to enter the account credentials (the username and password), from which the task will be run (this user account must have administrator privileges).
To run the task on behalf of SYSTEM, open its properties again and in the General tab click Change User or Group button. In Select User or Group Window specify system and save the changes.
schtasks /create /tn "automountvhdx" /tr "diskpart.exe /s 'c:\Scripts\attach_vhdx.txt'" /sc ONLOGON /ru SYSTEM
At the next Windows startup this specified VHDx disk will be automatically mounted.
If you want the vhdx image to be mounted not in a separate drive letter but into a folder, change the code in the c:\Scripts\attach_vhdx.txt file to:
select vdisk file="C:\vhd2\w10vhd.vhdx"
attach vdisk
select part 1
assign mount="c:\mount"
exit
As a result of this command, the virtual disk will be mounted in a separate directory on your physical drive (in this directory the contents of the vhdx file will be displayed).
You can mount not only a local VHD drive, but also a disk from the network shared folder using the UNC path:
select vdisk fils="\\srv1\share\win10shared.vhdx"
To test your vhdx automount script, you do not have to restart the computer each time, just run the following command as administrator:
diskpart /s c:\Scripts\attach_vhdx.txt
If your code doesn’t contain errors, then the command will connect the vhdx file and a new drive will appear in your system.
These ways to automate mounting of virtual disks can be used in Windows 10, Windows 8.1 and Windows 7.
If you prefer PowerShell, you can automatically mount the VHDX disk using the following PowerShell script at startup:
mount-vhd -path C:\vhd\win10vhd.vhdx –PassThru | Get-Disk | Get-Partition | Set-Partition -NewDriveLetter G
13 comments
Simple VHD Manager is portable freeware which helps VHD users simplify some of these operations:
– You can easily create VHD/VHDX files
– You can attach and detach VHD/VHDX/ISO files via drag and drop
– You can permanently attach a virtual hard disk in Windows 10 , Windows 8,1 , Windos 8, Windows 7
– You can easily add and/or remove VHD/VHDX/ISO files to the boot menu
http://www.sordum.org/8705/simple-vhd-manager-v1-1/
As always, answer is in the comments, thank you.
It broke my computer…
Virustotal found a couple of trojans in Simple VHD Manager
Yes all of them from China 🙂
Nice Article, thanks
Exactly what I was looking for; unfortunately it didn’t work for me!
I found that letting the Task Scheduler call diskpart with the added arguments did nothing for some reason: it executes the task on start-up, but without any results.
The work-around: In C:\Scripts I added another file ‘diskpart.cmd’ with the single line
diskpart /s “C:\Scripts\attach_vhd.txt”
In the Actions tab, I then simply called ‘C:\Scripts\diskpart.cmd’, instead, without any arguments.
This fixed the issue, although i have no idea what the problem was.
Thanks to Herbert and you others!
This has been a big problem for me! I did not get it to work, and have spent lot of time trying to solve it. But now it works!
I used a cmd-file “Attach_vhd.cmd” with the contents:
diskpart / s “C: \ Scripts \ attach_vhd.txt”
The file attach_vhd are as described above:
select VDisk file = “F: \ imagedisk.vhdx”
attach VDisk
assign letter = M
It works! Beware the sign ” that must be correct (Ascii 34), it can maybe be wrong when copying from the web-site.
“Beware the sign ” that must be correct (Ascii 34), it can maybe be wrong when copying from the web-site.”
Thank you for this tip! When I copied and pasted my Action argument into Notepad, I could see that the problem was with the incorrect quotation marks. After correcting this everything seems to be working. Finally!
Get rid of the quotes in the argument and this procedure works fine. Should be: / s C: \ Scripts \ attach_vhd.txt with no quotes. Delete the spaces in my argument if you’re copying and pasting . It also helps if you configure for Windows 7, if you’re using 7. And delay the task for one minute while everything else starts up.
Great article, simple and yet with all the necessary detail.
Thanks a lot, I happened to find this problem just yesterday when I rebooted a client’s machine and a VHDx wasn’t mounted. What a life saver 🙂
Cheers,
Ivo Pereira
Portugal
Ran into a bunch of issues with this article. Then found this one:
https://angler.wordpress.com/2010/03/11/windows-7-auto-mount-vhds-at-startup/
Worked without any issues at all and a little less complicated
You’re better off using PowerShell.
Manually mount the VHDX in Disk Management (diskmgmt.msc) and assign it a drive letter and volume label. Once you do this it will remember the drive letter and volume name the next time it is mounted.
Then create a .ps1 file and enter
Mount-DiskImage -ImagePath ‘D:\VHDS\MyVirtualDrive.vhdx’ -StorageType VHDX
amending where necessary for the file name and if it is different file extension such as VHD or ISO
Then create a scheduled task, set the trigger to be at startup. Set the action to run Powershell. In the arguments box add -File “D:\VHDS\AutoMountVHDX.ps1”