Technically Impossible

Lets look at the weak link in your statement. Anything "Technically Impossible" basically means we haven't figured out how yet.

Mount ISO and change its drive letter with PowerShell - Drive letter assignment to volume, not partition

Abstract

Windows 10 can mount ISO file, but user can't specify drive letter in advance of mounting. Its drive letter is dynamically assigned by Windows. If specific drive letter should be assigned, assigned drive letter must be changed after mounting.

This article introduces PowerShell script to mount ISO and change its drive letter.

Note

Scripts introduced in this post required administration privileges. If they runs on PowerShell, VSCode, Terminal, etc, these application must be "Run as administrator"

Run as administrator
f:id:espio999:20201202232301p:plain

Revisit the basics

device
disk
drive
hardware
virtual hardware
their abstraction
office floor
partition border that separates disk space partition board
volume partitioned disk space room
drive letter pointer to partition or volume room number

Concept of a device and disk is almost snynonymous. Although they are used synonymously as HDD, optical drive, etc, they refer to an abstract equipment regardless of physical or logical. This is because there are cased where multiple physical HDD are logically formed into a single drive as RAID, and there are cases where is it a virtual disk file that is mounted as a storage of virtual server. Here, "disk" is all-inclusive term of them.

Disk space is separated according to the needs. A separation (border) of partitioned space is called "partition" It refers only border, doesn't include space. Partitioned space is called "volume".

And drive letter is assigned to partition or volume. It works as mount point of partition or assigned disk space.

If disk is an office floor, partition is a partition board, volume is a room, and drive letter is room number.

In PowerShell, drive letter is operated at the level of partition. And a drive mounting ISO file has only volume without partition.

Approach - Bad pattern

Following process is normal approach, but it doesn't work for a drive mounting ISO file. There are no partition.

  1. Mount ISO file
  2. Get its volume
  3. Get its partition
  4. Change drive letter of the partition

Output
f:id:espio999:20201202232409p:plain

ISO is mounted at Z drive. Although a volume is referable, partition is not. Z drive is not listed as partitions. As conclusion, changing a drive letter with "Set-Partition" doesn't work here.

Approach - Good pattern

To operate a drive letter at the volume level, available options are

  • diskpart
  • mountvol
  • WMI object

Following process is the case with WMI object, and this process works.

  1. Mount ISO file
  2. Get its volume
  3. Identify assigned drive letter
  4. Change a drive letter through WMI object

Explain codes in order. First, define the absolute path of the ISO file in myISO and mount it.

$myISO = 'E:\ISO\Microsoft Bookshelf Basic\BSBASIC2.ISO'
mount-diskimage $myISO

Get the volume of ISO file, and identify assigned drive letter.
In PowerShell, drive letter is expressed in a single character as "X" and "Y", but WMI handles it with ":" as "X:". Be careful.
And define new drive letter.

$vol = Get-DiskImage $myISO | Get-Volume
$old_drv = $vol.DriveLetter + ':'
$new_drv = 'X:'

Then, get the list of volume information as a WMI object.
From the acquired volume information, identify the volume with assigned drive letter, and instantiate it with the drive letter newly defined.
This series of flows is executed in a single line via pipe.

Get-WmiObject -Class Win32_Volume | Where-Object {$_.DriveLetter -eq $old_drv} | Set-WmiInstance -Arguments @{DriveLetter=$new_drv}

Output
f:id:espio999:20201202232455p:plain

As its result, the drive letter is changed to "X".
If these process should be executed as regular routine, same them as a script file and register in Task Scheduler.

Code

Bad pattern

gist.github.com

Good pattern

gist.github.com