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