28

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.

46

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.

running Mount-DiskImage cmdlet

Tada! It's mounted:

Tada! It’s mounted — “Devices with Removable Storage”

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.

“Devices with Removable Storage” after dismounting ISO

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

output of foreach/Get-Volume and ls

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.

using <code>-PassThru</code> and <code>Get-DiskImage</code>

The StorageType of a VHD is 2, where an ISO will return 1. Here's the output of $UbuntuISO:
Here's the output of <code>$UbuntuISO</code>

This is the output of $temp after mounting a VHD: (Mount-DiskImage can also mount VHDs!)
the output of <code>$temp</code> after mounting a VHD

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:
the <code>$UbuntuISO</code> variable does not stay updated


Technet: Mount-DiskImage

Technet: Dismount-DiskImage

  • 6
    Damn, with all those new cmdlets in PowerShell Windows 8 starts to tickle. – sinni800 Nov 4 '12 at 3:38
  • 1
    We can easily mount the image through the command C:\>explorer.exe imagepath, here we don't need of PS but for unmount there we have need the PS. – avirk Nov 4 '12 at 5:49
  • 3
    Is all inside Windows 8? I mean don't you need to install other programs??? – Searush Nov 4 '12 at 6:10
  • 2
    +1 Very nice indeed! This was precisely what I was hoping to find in Win8 so I could eventually add it to the end of my answer here, but you saved me the trouble. I really need to start boning up on my PS skills. :) Edit: Is there any cmdlet to test the innards and report whether the file is an ISO or VHD or something else entirely that's not supported? Or maybe a way to test the return value of Mount-DiskImage perhaps so code can be branched accordingly? – Karan Nov 4 '12 at 15:38
  • 1
    @Karan Finally got around to figuring that out =D It's not going to test the guts, and frankly I think that's overkill. But it will let us know if it's mounted and an ISO vs VHD without diving in to PowerShell error handling. – Tanner Faulkner Nov 9 '12 at 16:17
3

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).

  • What does the -Command switch do, and why run non-interactive? – Tanner Faulkner Nov 4 '12 at 17:14
  • 1
    oops, didn't need -Command, it appears to do that automatically, non-interactive because chances are, a script is doing it rather than a human (who can just use the GUI) – ronalchn Nov 4 '12 at 19:25
  • Don't forget the simpler dismount by filename as well: PowerShell Dismount-DiskImage \"C:\AbsolutePathTo\Image.iso\" – juanitogan Jan 21 '16 at 1:14
  • For current dir mounts: PowerShell Mount-DiskImage \"%CD%\Image.iso\" – juanitogan Jan 21 '16 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.