#PSTip Working with ISO files

Note: This tip requires PowerShell 3.0 and Windows 8 or above.

Starting with Windows 8, mounting virtual hard disks or ISO files is not an issue anymore. The operation is built into the system. When mounted, a virtual hard disk or ISO file gets a drive letter assigned to it and it appears as a normal disk drive in Windows Explorer. To mount ISO files, simply double click them. To dismount, right click the drive and chose ‘Eject’.

In PowerShell, we can manage those drives via the Storage module and its *-DiskImage cmdlets. For example, to mount an ISO image:

PS> Mount-DiskImage -ImagePath 'D:\ISO\en_windows_8_ent_x64.iso' -PassThru

Attached          : False
BlockSize         : 0
DevicePath        :
FileSize          : 3490912256
ImagePath         : D:\ISO\en_windows_8_ent_x64.iso
LogicalSectorSize : 2048
Number            :
Size              : 3490912256
StorageType       : 1
PSComputerName    :

By default, Mount-DiskImage does not generate any output, so add the PassThru switch to get a result.

PS> Dismount-DiskImage -ImagePath 'D:\ISO\en_windows_8_ent_x64.iso' -PassThru

Attached          : True
BlockSize         : 0
DevicePath        : \\.\CDROM1
FileSize          : 3490912256
ImagePath         : D:\ISO\en_windows_8_ent_x64.iso
LogicalSectorSize : 2048
Number            : 1
Size              : 3490912256
StorageType       : 1
PSComputerName    :

How about getting a list of mounted files? Let’s try the Get-DiskImage cmdlet.

PS> Get-DiskImage

cmdlet Get-DiskImage at command pipeline position 1
Supply values for the following parameters:
ImagePath[0]:

Bummer! Instead of getting mounted images we are prompted to enter the path of the image file! So, to bypass that annoying request and get the images we get a list of volumes and pipe them to the Get-DiskImage cmdlet:

PS> Get-Volume | Get-DiskImage
Attached          : True
BlockSize         : 0
DevicePath        : \\.\CDROM1
FileSize          : 3618824192
ImagePath         : D:\ISO\en_windows_8_ent_x64.iso
LogicalSectorSize : 2048
Number            : 1
Size              : 3618824192
StorageType       : 1
PSComputerName    :

Attached          : True
BlockSize         : 0
DevicePath        : \\.\CDROM2
FileSize          : 3490912256
ImagePath         : D:\ISO\mu_exchange_server_2013_x64.iso
LogicalSectorSize : 2048
Number            : 2
Size              : 3490912256
StorageType       : 1
PSComputerName    :

If you take a second look at the output of each command you will notice that the result doesn’t reflect the action of the command. The output shows the state of the image object before it has been mounted or dismounted. There’s an open bug report on Connect if you want to vote on it.

Finally, using that trick we can quickly dismount all mounted (iso based) drives:

PS> Get-Volume | Get-DiskImage | Dismount-DiskImage
Share on: