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

July 25, 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
0
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

Configuring Event Viewer Log Size on Windows

May 24, 2023

How to Detect Who Changed the File/Folder NTFS...

May 24, 2023

How to Create, Change, and Remove Local Users...

May 17, 2023

Fix: BSOD Error 0x0000007B (INACCESSABLE_BOOT_DEVICE) on Windows

May 16, 2023

View Success and Failed Local Logon Attempts on...

May 2, 2023

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

  • Configuring Event Viewer Log Size on Windows

    May 24, 2023
  • How to Detect Who Changed the File/Folder NTFS Permissions on Windows?

    May 24, 2023
  • Enable Single Sign-On (SSO) Authentication on RDS Windows Server

    May 23, 2023
  • Allow Non-admin Users RDP Access to Windows Server

    May 22, 2023
  • How to Create, Change, and Remove Local Users or Groups with PowerShell?

    May 17, 2023
  • Fix: BSOD Error 0x0000007B (INACCESSABLE_BOOT_DEVICE) on Windows

    May 16, 2023
  • View Success and Failed Local Logon Attempts on Windows

    May 2, 2023
  • Fix: “Something Went Wrong” Error When Installing Teams

    May 2, 2023
  • Querying Windows Event Logs with PowerShell

    May 2, 2023
  • Configure Windows LAPS (Local Administrator Passwords Solution) in AD

    April 25, 2023

Follow us

  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Booting Windows 7 / 10 from GPT Disk on BIOS (non-UEFI) systems
  • Error Code: 0x80070035 “The Network Path was not found” after Windows 10 Update
  • 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
Footer Logo

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


Back To Top