Justin Dearing
Justin Dearing

Reputation: 14978

How do I get the drive letter for the ISO I mounted with Mount-DiskImage

I have an ISO I mount via the Mount-DiskImage command. However, I don't know how to get the drive letter for the mounted disk Image. I try $mountResult = Mount-DiskImage D:\ISOs\clonezilla-live-1.2.12-10-i486.iso -PassThru. None of the information that is returned is the drive letter as illustrated below:

PS C:\Windows\system32> $mountResult | fl *


Attached              : False
BlockSize             : 0
DevicePath            : 
FileSize              : 110100480
ImagePath             : D:\ISOs\clonezilla-live-1.2.12-10-i486.iso
LogicalSectorSize     : 2048
Number                : 
Size                  : 110100480
StorageType           : 1
PSComputerName        : 
CimClass              : ROOT/Microsoft/Windows/Storage:MSFT_DiskImage
CimInstanceProperties : {Attached, BlockSize, DevicePath, FileSize...}
CimSystemProperties   : Microsoft.Management.Infrastructure.CimSystemProperties

PS C:\Windows\system32> $mountResult | select -ExpandProperty CimSystemProperties | fl *


Namespace  : ROOT/Microsoft/Windows/Storage
ServerName : ECHO-BASE
ClassName  : MSFT_DiskImage
Path       : 

Calling Get-DiskImage D:\ISOs\clonezilla-live-1.2.12-10-i486.iso after doesn't return the drive letter either.

How do I get the drive letter?

Upvotes: 36

Views: 53082

Answers (9)

Yosef Blass
Yosef Blass

Reputation: 21

( Mount-DiskImage -ImagePath $ImagePath | Get-Volume ).DriveLetter

You may also try this one liner

Upvotes: 2

Ephasius
Ephasius

Reputation: 96

Only started looking at Powershell today when I heard a colleague ask the same question. If it helps, I pieced it together into a 1-liner. Posted this on another thread too where they were asking a similar question:

mountvol "Y:" (Mount-DiskImage - ImagePath "c:\x.iso" -NoDriveLetter | Get-Volume).UniqueId

Then to dismount:

Dismount-DiskImage -ImagePath "c:\x.iso"

Best regards

Upvotes: 0

Dustin
Dustin

Reputation: 2154

I'm mounting a VHD and the accepted answer didn't work. If I pipe the result through a couple of extra commands it does.

$mountResult = Mount-DiskImage C:\some.VHD -PassThru
($mountResult | Get-Disk | Get-Partition | Get-Volume).DriveLetter

I'm a PowerShell noob, so I might be doing something wrong or maybe it's because I'm using a VHD, not an ISO.

Upvotes: 1

James
James

Reputation: 51

I found this to work

$beforeMount = (Get-Volume).DriveLetter

$mountResult = Mount-DiskImage $imagePath

$setuppath = (compare $beforeMount (Get-Volume).DriveLetter -PassThru) + ":\"

Upvotes: 5

rubberduck
rubberduck

Reputation: 41

hmm.. why so complicated?

Mount-DiskImage D:\cd.iso -PassThru | Get-Volume

Upvotes: 4

isaacparrot
isaacparrot

Reputation: 2053

Try this:

$mountResult = Mount-DiskImage D:\ISOs\clonezilla-live-1.2.12-10-i486.iso -PassThru
$mountResult | Get-Volume

This will return which drive letter that ISO is assigned to along with other info -- from there it's just a matter of parsing the output.

EDIT: This will return JUST the drive letter:

$driveLetter = ($mountResult | Get-Volume).DriveLetter

Upvotes: 68

Thom Schumacher
Thom Schumacher

Reputation: 1583

This worked for me:

$beforeMount = (Get-Volume).DriveLetter
$imagePath = 'C:\dsc\en_windows_server_2016_x64_dvd_9718492.iso'
$mountResult = Mount-DiskImage $imagePath
$afterMount = (Get-Volume).DriveLetter
$setuppath = "$(($afterMount -join '').replace(($beforeMount -join ''), '')):\"

Upvotes: 0

RzzRBladez
RzzRBladez

Reputation: 21

I am not entirely sure whether it belongs here, but considering the question and the answers I would say it does.

Italics: copied from the COMMAND PROMPT.

Assumptions

When we talk about mounting we usually talk about mounting (virtual) disks, for which we have DISKPART.EXE, or installation files like .WIM and .SWM, for which we have DISM.EXE (IMAGEX.EXE), or third party software like 'NTLite', or it concerns CD and DVD images like .ISO. As far as I know .ISO-files are always mounted read only, unless third party software is used. As such .ISO-files are the most inflexible files and thus I wrote this from the assumption that the question was asked at the time to provide a correct and complete path to the source-file for one or more copy commands, a find command, or a read command, each of them depending on a well-defined path to a source-file, not necessarily the path to the mounted image.

One Possible Solution#

c:\windows\system32>powershell.exe mount-diskimage -imagepath
"d:\blah\vlah.iso" -confirm -passthru

Attached           : True
Blocksize          : 0
Devicepath         : \\.\CDROM0 <<== Object of interest, since COPY.EXE
Filesize           : 4586569728           won't recurse and XCOPY.EXE
Imagepath          : "d:\blah\vlah.iso"   won't accept it as a valid 
Logicalsectorsize  : 2048                 path, however ROBOCOPY.EXE
Number             : 0                 will accept it and then it does 
Size               : 4586569728        do what I demand of it, copy the  
Storagetype        : 1                 contents of the  mounted .ISO
Pscomputername     :

xcopy \\.\CDROM0\*.* C:\new /h /i /c /k /e /r /y /f /b
Invalid drive specification
0 File(s) copied

Robocopy \\.\CDROM0 C:\new /E /ZB /COPYALL /256 /R:3 /W:1 /X /V /TS  
/FP /NP /ETA /LOG:w:\ROBO.LOG /TEE /NJH

Arguably, this combination is the most effective to both answer the question, and solve the problem since POWERSHELL.EXE won't open a 'gui',(so all feedback 'stdout' and 'stderr', when allowed, will appear in one window), while it delivers us 'stdout' that makes the necessity to obtain a drive letter superfluous to your file and/or command a waste of time. If I am correct the 'devicepath' will always be the same, though I have not checked that, and I cannot say whether that is only on my computer the case.

Issue Concerning The Use Of POWERSHELL.EXE Via The COMMAND PROPMT

One issue though, it might be my computer, but better repeat the POWERSHELL.EXE command, because with me and my batch-files most of the times 'Attached : False' is the outcome of the first try, both with mounting, (false), as well as 'unmounting' the image, (true). That will cause an error in the execution of my or your 'Batch-file' that looks something like: 'The system cannot find the path specified.' That will follow the moment another command will reference the intended 'mountpoint' and image.

ADDENDUM 05-feb-2017: I found out that the solution of using \.\CDROMX seems to only work when one adds /256 otherwise it may fail with the following message:

2017/02/05 01:11:15 ERROR 53 (0x00000035) Accessing Source Directory \.\CDROM0\ The network path was not found.

I hope this helps people, this helped me already, following notepad long line setting: 72 creates an ugly layout on stackoverflow.

Upvotes: 2

akhil vangala
akhil vangala

Reputation: 1063

FYI I had an issue mounting same image again so I made a small change, which checks if image is already mounted if not mounts and give the volume.

$ImagePath= " " ## Path of ISO image to be mounted 
$ISODrive = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter
IF (!$ISODrive) {
Mount-DiskImage -ImagePath $ImagePath -StorageType ISO
}
$ISODrive = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter
Write-Host ("ISO Drive is " + $ISODrive)

Upvotes: 3

Related Questions