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 automatically assigns either the next available or -NoDriveLetter (using this parameter)

Can Anyone direct me how to accomplish this???

  • 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:\

     

  • 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:\

     

    • CesarChen's avatar
      CesarChen
      Copper Contributor

      Wow... very thorough script... more complex than I expected.  I would've hope that it was easier... such as a single line command as:

      Powershell -command "Mount-DiskImage -ImagePath ""c:\image\image.iso"" -DriveLetter F

      Instead, there is no -DriveLetter parameter for Mount-DiskImage...

      I am grateful for your assistance.

       

      • It's a lot of error-handling, but yes 😅 The ISO is mounted by default, but if you want a different drive letter... Then you have to do more work 😅

        Please mark my answer as Solution if it works for you to mark it as solved 

Resources