In Windows 7 and Windows Server 2008 R2 it became possible to create and mount virtual hard disk files (VHD) 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 files manually each time you reboot the system. Only computer administrators have the right to mount VHD files, users do not have these privileges.
In this article we’ll show how to automatically mount a VHD file at the system startup so that virtual hard disks are available for users (with no administrator privileges).
Windows doesn’t have a built-in ability to automatic mounting of VHD files when you reboot your system. Let’s try to implement auto-mount drives using the diskpart and Windows Task Scheduler.
So, create a new text file (C:\Scripts\attach_vhd.txt) containing the following text:
select vdisk file="F:\imagedisk.vhdx" attach vdisk assign letter=K
F:\imagedisk.vhdx is the full path to the VHD or VHDX disk image
K is a drive letter to be assigned to your VHD disk.
Run Windows Task Scheduler (Taskschd.msc) and create a new Scheduler task (Actions -> Create Task). Configure it as follows:
- Specify the name of the task (Name)
- Check Run whether user is logged on or not
- Check Run with highest privileges
- Add a new startup trigger (At Startup) with the default settings
- Create a new action (New)
- The action is Start a program
- Program/script: diskpart
- As a program argument, specify : /s “C:\Scripts\attach_vhd.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 name and the password of the account, from which the task will be run. (The account must have administrator privileges.)
To run the task as 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.
At the next startup, this VHD disk will be automatically mounted.