How can I mount an ISO via PowerShell/programmatically?

28

15

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.

Tanner Faulkner

Posted 2012-11-04T03:20:56.543

Reputation: 11 948

Answers

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

Tanner Faulkner

Posted 2012-11-04T03:20:56.543

Reputation: 11 948

That is an exemplary answer! – Peter Mortensen – 2016-03-16T22:11:42.413

See here for mounting an iso as a directory (instead of drive letter).

– user2426679 – 2018-03-21T21:45:22.413

6Damn, with all those new cmdlets in PowerShell Windows 8 starts to tickle. – sinni800 – 2012-11-04T03:38:31.467

1We 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 – 2012-11-04T05:49:43.760

3Is all inside Windows 8? I mean don't you need to install other programs??? – Searush – 2012-11-04T06:10:05.493

@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... – Tanner Faulkner – 2012-11-04T15:28:46.747

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 – 2012-11-04T15:38:57.637

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 – 2012-11-09T16:17:50.393

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

ronalchn

Posted 2012-11-04T03:20:56.543

Reputation: 4 421

Don't forget the simpler dismount by filename as well: PowerShell Dismount-DiskImage \"C:\AbsolutePathTo\Image.iso\" – juanitogan – 2016-01-21T01:14:48.497

For current dir mounts: PowerShell Mount-DiskImage \"%CD%\Image.iso\" – juanitogan – 2016-01-21T01:23:35.413

What does the -Command switch do, and why run non-interactive? – Tanner Faulkner – 2012-11-04T17:14:10.723

1oops, 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 – 2012-11-04T19:25:31.317