windowspowershellcmdroaming-profile

Mapping Hard Disk Image File to partition using command line


I have a directory with hundreds of Hard Disk Image Files (.vhdx) - each containing user profile directory (C:\Users\xxx). When I click on a file using File Explorer, it is automatically mounted and I can access files inside. I have an issue with recreating the same with a command.

I do not know what is the username with given SID - I just need to modify a single file in each profile.

Example file: E:\profiles\UVHD-S-1-5-21-1614895754-1035525444-839522115-21214.vhdx

I've tried to use net use, dism and subst however I couldn't get any of those to work.


Solution

  • net use is for mapping SMB shares, dism is for working with .wim images, subst is for mapping local paths to drive letters. Neither of them is suitable for working with VHD(X) files. Or claims to be, for that matter.

    In more recent Windows versions (starting with Windows 8 IIRC) you have cmdlets like Mount-DiskImage or Mount-VHD for working with Hyper-V virtual harddisks. On Windows 7 you need to use diskpart.exe.

    Attach a virtual harddisk:

    @'
    select vdisk file="C:\path\to\your.vhdx"
    attach vdisk
    '@ | diskpart
    

    Detach a virtual harddisk:

    @'
    select vdisk file="C:\path\to\your.vhdx"
    detach vdisk
    '@ | diskpart