Forum Discussion

CesarChen's avatar
CesarChen
Copper Contributor
Nov 14, 2024
Solved

Newbie here...

Using Windows 11 Pro version 23H2 ... Am trying to use the Mount-DiskImage command to mount an .iso file, but can't figure out how to do it assigning it a specific drive letter... it seems that it au...
  • Harm_Veenstra's avatar
    Nov 18, 2024

    You can first mount the ISO and then connect a drive letter to this, and I wrote this script for that:

    #Requires -RunAsAdministrator
    param (
      [Parameter(Mandatory = $true)][String]$ISOFileName,
      [Parameter(Mandatory = $true)][String]$DriveLetter
    )
    
    #Check if DriveLetter is in use
    if (Test-Path -LiteralPath "$($DriveLetter):") {
      Write-Warning ("Specified driveletter {0}: is in use, exiting..." -f $DriveLetter)
      return
    }
    
    #Check if $IsoFileName was already mounted
    if ((Get-DiskImage -ImagePath $ISOFileName).Attached) {
      Write-Warning ("ISO {0} already mounted, exiting..." -f $ISOFileName)
      return
    }
    
    #Mount first
    if (Test-Path -LiteralPath $ISOFileName) {
      try {
        $Mount = Mount-DiskImage -ImagePath $ISOFileName -NoDriveLetter -StorageType ISO -Access ReadOnly -ErrorAction Stop
      }
      catch {
        Write-Warning ("Error mounting {0}, exiting..." -f $ISOFileName)
        return
      }
    }
    
    #Assign driveletter
    try {
      mountvol.exe "$($DriveLetter):" $($mount | Get-Volume).UniqueId
      Write-Host ("Mounted {0} at {1}:" -f $ISOFileName, $DriveLetter) -ForegroundColor Green
    }
    catch {
      Write-Warning { "Error mounting {0} at {1}:, dismounting {2}" -f $ISOFileName, $DriveLetter, $ISOFileName }
      try {
        Dismount-DiskImage -ImagePath $ISOFileName -ErrorAction Stop
        Write-Host { "Dismounted {0}..." -f $ISOFileName }
      }
      catch {
        Write-Warning ("Error dismounting {0}, Check Diskmanagement" -f $ISOFileName)
      }
    }

     

    Save the script to c:\scripts, as mount-iso.ps1, for example. Then run it as Administrator, as c:\scripts\mount-iso.ps1 -ISOFileName c:\data\iso\isofilename.iso -DriveLetter X to mount c:\data\iso\isofilename.iso as X:\

     

Resources