How can I mount an ISO via PowerShell/programmatically?

62,737

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.

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

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

Share:
62,737

Related videos on Youtube

rtf
Author by

rtf

Updated on September 18, 2022

Comments

  • rtf
    rtf over 1 year

    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.

  • avirk
    avirk over 11 years
    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.
  • Searush
    Searush over 11 years
    Is all inside Windows 8? I mean don't you need to install other programs???
  • rtf
    rtf over 11 years
    @SEARAS Yes, the Mount-DiskImage cmdlet was modified to use ISO files in Windows 8 and Server 2012. There's no need for 3rd party programs. A long overdue Windows feature in my opinion...
  • Karan
    Karan over 11 years
    +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?
  • rtf
    rtf over 11 years
    What does the -Command switch do, and why run non-interactive?
  • ronalchn
    ronalchn over 11 years
    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)
  • rtf
    rtf over 11 years
    @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.
  • juanitogan
    juanitogan over 8 years
    Don't forget the simpler dismount by filename as well: PowerShell Dismount-DiskImage \"C:\AbsolutePathTo\Image.iso\"
  • juanitogan
    juanitogan over 8 years
    For current dir mounts: PowerShell Mount-DiskImage \"%CD%\Image.iso\"
  • Peter Mortensen
    Peter Mortensen about 8 years
    That is an exemplary answer!
  • user2426679
    user2426679 about 6 years
    See here for mounting an iso as a directory (instead of drive letter).