How can I mount an ISO via PowerShell/programmatically?
How can I mount and dismount ISO images from PowerShell in Windows 8 without 3rd party programs?
This question's original revision got me wondering if it's possible to mount an ISO via PowerShell in Windows 8.
Solution 1:
Mount an ISO from command prompt (Windows 8/2012 and above only)
If you're sitting at a command prompt and need to mount an ISO, run the following command:
PowerShell Mount-DiskImage
This will invoke a PowerShell cmdlet. You will be prompted for the path of the ISOs you wish to mount. When you are done, leave the last one blank and push Enter.
Tada! It's mounted:
Dismount an ISO
To dismount an ISO from PowerShell run Dismount-DiskImage
and follow the prompt. If you know only the drive letter off the top of your head, and not the image path, use this command:
Get-Volume [Drive Letter] | Get-DiskImage | Dismount-DiskImage
This command will grab the drive you specify, find the disk image, and dismount it.
Mounting multiple ISOs and displaying drive letters
You can also use the -PassThru
flag to store data passed to the command. Let’s mount a few ISOs, display their drive letters, execute a file on one of the drives, and then dismount all the ISOs.
Mount the ISOs
$MountedISOs=Mount-DiskImage -PassThru D:\Downloads\Ubuntu.iso,D:\Downloads\Windows8.iso,D:\Downloads\Server2012.iso
Display volume info for each ISO mounted using a foreach
loop
foreach($iso in $MountedISOs){Get-Volume -DiskImage $iso}
List J drive
ls J:\
Open a file
start wubi.exe
To dismount the ISOs, use the following command:
Dismount-DiskImage $MountedISOs.ImagePath
Testing the ISO
To build a simple script that checks if the ISO is attached and is in fact an ISO (vs. a VHD) I like to use -PassThru
to store the object temporarily, and use the Get-DiskImage
command to update the status of the DiskImage object. This will update the Attached
property. The StorageType
property will tell you whether the file is an ISO or VHD according to its file extension.
The StorageType
of a VHD is 2, where an ISO will return 1.
Here's the output of $UbuntuISO
:
This is the output of $temp
after mounting a VHD: (Mount-DiskImage
can also mount VHDs!)
Note that the Attached
property above is False
,
despite the Mount-DiskImage
command running without a hitch.
Keep in mind that the $UbuntuISO
variable will not stay updated either:
Technet: Mount-DiskImage
Technet: Dismount-DiskImage
Solution 2:
Normally, if you want to do this via the command line, you need a non-interactive method. You will want to use the -ImagePath
switch to do this.
Thus, the command is:
PowerShell Mount-DiskImage -ImagePath \"C:\AbsolutePathTo\Image.iso\"
Remember that if you quote the absolute path (for containing spaces and other special characters), you need to escape the quotes.
To dismount an iso image, remember to quote it:
PowerShell "Get-Volume G | Get-DiskImage | Dismount-DiskImage"
Note that we did not have to quote the command in the first case, but we do in the second, because the |
pipe will cause the command line to think that it is in the command line context, rather than addition arguments to PowerShell
(which PowerShell automatically concatenates into a single command).
Also make sure to spell Dismount-DiskImage
correctly (no k
in Dismount).