Context Menu : Mount VHD as V

Discussion in 'Scripting' started by retest, Sep 7, 2023.

  1. retest

    retest MDL Member

    Nov 2, 2010
    116
    63
    10
    #1 retest, Sep 7, 2023
    Last edited: Sep 8, 2023
    I often mount VHDs and ISOs and prefer them to be mounted as V and X.

    I came up with the script below.

    I'm not too familiar with PowerShell. Any suggestions.comments?

    - I don't want to call a external script/program from the registry. (code should be in the registry entry)
    - I don't want to install PS 7.
    - I don't want any dependencies e.g. Imdisk.

    Is there any other (better) way to do this ?

    Code:
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.VhdFile\shell\Mount VHD as V\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command ""foreach ($l in 'V:\','W:\','Y:\','Z:\') {if ( -not (Test-Path -Path $l)) {$d = $l.substring(0,2) ; break}} ; mountvol $d (Mount-DiskImage -ImagePath '%%1' -NoDriveLetter -PassThru | Get-Disk | Get-Partition | Get-Volume).UniqueId""" /f
    
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.IsoFile\shell\Mount ISO as X\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command ""foreach ($l in 'X:\','Y:\','Z:\') {if ( -not (Test-Path -Path $l)) {$d = $l.substring(0,2) ; break}} ; mountvol $d (Mount-DiskImage -ImagePath '%%1' -NoDriveLetter | Get-Volume).UniqueId""" /f
    
    
    -------------------

    These work too:

    for VHDs:
    Code:
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.VhdFile\shell\Mount VHD as V\command" /ve /t REG_SZ /d "cmd /c ""@ECHO OFF & FOR %%%%I IN (V W U T S R) DO IF NOT EXIST %%%%I:\ ((echo select vdisk file=%%1 & echo attach vdisk & echo select partition 1 & echo assign letter=%%%%I & echo exit) | diskpart  > nul 2>&1 & exit)""" /f
    
    and

    Code:
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.VhdFile\shell\Mount VHD as V\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command ""foreach ($l in 'V:\','W:\','Y:\','Z:\') {if ( -not (Test-Path -Path $l)) {$d = $l.substring(0,1) ; break}} ; Get-Partition(Mount-DiskImage -ImagePath '%%1' -NoDriveLetter).Number |  Set-Partition -NewDriveLetter $d""" /f
    
    EDIT : Yes, I get a flashing PS window.

    EDIT : See next post.
     
  2. retest

    retest MDL Member

    Nov 2, 2010
    116
    63
    10
    #2 retest, Sep 8, 2023
    Last edited: Sep 20, 2023
    (OP)
    The code posted above 'worked' on Win 11.
    But not on Win 10. I had got the escape characters wrong in the reg add command.

    Corrected code :

    To install
    Code:
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.VhdFile\shell\Mount VHD as V\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"foreach ($l in 'V:\','W:\','Y:\','Z:\') {if ( -not (Test-Path -Path $l)) {$d = $l.substring(0,2) ; break}} ; mountvol $d (Mount-DiskImage -ImagePath '%%1' -NoDriveLetter -PassThru ^| Get-Disk ^| Get-Partition ^| Get-Volume).UniqueId\"" /f
    
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.VhdFile\shell\Mount VHD as V" /v "Icon" /t REG_EXPAND_SZ /d "%%SystemRoot%%\System32\imageres.dll,-32" /f
    
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.IsoFile\shell\Mount ISO as X\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"foreach ($l in 'X:\','Y:\','Z:\') {if ( -not (Test-Path -Path $l)) {$d = $l.substring(0,2) ; break}} ; mountvol $d (Mount-DiskImage -ImagePath '%%1' -NoDriveLetter ^| Get-Volume).UniqueId\"" /f
    
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.IsoFile\shell\Mount ISO as X" /v "Icon" /t REG_EXPAND_SZ /d "%%SystemRoot%%\System32\imageres.dll,-5205" /f
    
    
    UPDATE : Corrected mountvol argument :

    Code:
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.VhdFile\shell\Mount VHD as V\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"foreach ($l in 'V:\','W:\','Y:\','Z:\') {if ( -not (Test-Path -Path $l)) {$d = $l ; break}} ; mountvol $d (Mount-DiskImage -ImagePath '%%1' -NoDriveLetter -PassThru ^| Get-Disk ^| Get-Partition ^| Get-Volume).UniqueId\"" /f
    
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.VhdFile\shell\Mount VHD as V" /v "Icon" /t REG_EXPAND_SZ /d "%%SystemRoot%%\System32\imageres.dll,-32" /f
    
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.IsoFile\shell\Mount ISO as X\command" /ve /t REG_SZ /d "powershell -windowstyle hidden -command \"foreach ($l in 'X:\','Y:\','Z:\') {if ( -not (Test-Path -Path $l)) {$d = $l ; break}} ; mountvol $d (Mount-DiskImage -ImagePath '%%1' -NoDriveLetter ^| Get-Volume).UniqueId\"" /f
    
    Reg.exe add "HKCU\SOFTWARE\Classes\Windows.IsoFile\shell\Mount ISO as X" /v "Icon" /t REG_EXPAND_SZ /d "%%SystemRoot%%\System32\imageres.dll,-5205" /f
    
    
    To uninstall
    Code:
    Reg.exe DELETE "HKLM\SOFTWARE\Classes\Windows.VhdFile\shell\Mount VHD as V"  /f
    Reg.exe DELETE "HKCU\SOFTWARE\Classes\Windows.VhdFile\shell\Mount VHD as V"  /f
    
    Reg.exe DELETE "HKLM\SOFTWARE\Classes\Windows.IsoFile\shell\Mount ISO as X"  /f
    Reg.exe DELETE "HKCU\SOFTWARE\Classes\Windows.IsoFile\shell\Mount ISO as X"  /f
    
     
  3. retest

    retest MDL Member

    Nov 2, 2010
    116
    63
    10