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

Refer 2023 edition. This post is 2020 edition.

The WMI command-line (WMIC) utility is deprecated as of Windows 10, version 21H1, and as of the 21H1 semi-annual channel release of Windows Server.

wmic | Microsoft Learn
Due to deprecation of WMIC utility for 21H1 of Windows 10 and Windows Server (semi-annual edition), this article was reposted as 2023 edition. Please refer to the new edition here.
impsbl.hatenablog.jp

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


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

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

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