Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Windows 10 / Auto-Mount a VHD/VHDX File at Startup in Windows 10, 8.1

August 3, 2018 Windows 10Windows 8

Auto-Mount a VHD/VHDX File at Startup in Windows 10, 8.1

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

automount vhdx at startup using diskpart in Windows 10

  • 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.

Note. For security reasons, it’s better to change NTFS permissions for file “C:\Scripts\attach_vhdx.txt”, so that users could not change this file.

Run Windows Task Scheduler (Taskschd.msc) and create a new Scheduler task (Actions -> Create Task). Configure it as follows:

automount vhdx image task

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.

trigger to run on windows startup

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”.

diskpart mount vhdx using txt script

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).

admin credentials for run task

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.

run task from system account

You can create a scheduler task using PowerShell or using schtasks:

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.

mounted vhdx drive in disk managment console

Note. Typically, the drive appears in the list of available disks in about 30-60 seconds after Windows startup.

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).

mount vhdx file to a folder

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.

testing diskpart

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

mount-vhd using powershell

13 comments
1
Facebook Twitter Google + Pinterest
previous post
RDP Scaling Issue on High-DPI Displays in Windows 10
next post
Configuring Kerberos Authentication in Different Browsers

Related Reading

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

How to Use Ansible to Manage Windows Machines

September 25, 2023

Installing Language Pack in Windows 10/11 with PowerShell

September 15, 2023

How to View and Change BIOS (UEFI) Settings...

September 13, 2023

13 comments

thunderbold January 23, 2016 - 10:15 am

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/

Reply
Mitchell Stringer January 1, 2019 - 7:34 pm

As always, answer is in the comments, thank you.

Reply
buddieminecraft February 19, 2019 - 9:02 pm

It broke my computer…

Reply
Treeant34 May 20, 2016 - 12:18 am

Virustotal found a couple of trojans in Simple VHD Manager

Reply
thunderbold July 8, 2016 - 9:16 pm

Yes all of them from China 🙂  

Reply
Ben December 29, 2016 - 4:23 am

Nice Article, thanks

Reply
Herbert Quain January 27, 2017 - 11:45 pm

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.

Reply
Arne February 20, 2017 - 4:14 pm

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.

Reply
Todd November 14, 2017 - 8:16 am

“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!

Reply
Zoan March 15, 2017 - 11:38 am

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.

Reply
Ivo Pereira March 24, 2017 - 9:44 am

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

Reply
Tyler September 12, 2018 - 12:32 pm

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

Reply
Jamie Sandell June 17, 2019 - 8:45 am

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”

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Booting Windows 7 / 10 from GPT Disk on BIOS (non-UEFI) systems
  • Removable USB Flash Drive as Local HDD in Windows 10 / 7
  • How to increase KMS current count (count is insufficient)
  • How to Disable UAC Prompt for Specific Applications in Windows 10?
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Managing Printers from the Command Prompt in Windows 10 / 8.1
  • Fix: RDP Authentication Error Has Occurred – The Function Requested Is Not Supported
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top