How to Mount or Unmount an ISO File in Batch Script
ISO files are digital disc images, perfect replicas of an optical disc like a CD, DVD, or Blu-ray. Modern versions of Windows (Windows 8 and newer) have built-in support for "mounting" these ISO files, which creates a virtual drive in "This PC," allowing you to access the contents of the ISO as if it were a physical disc. This is an extremely useful feature for automating software installations or accessing archived data without needing to burn a disc.
This guide will teach you how to use the modern, standard PowerShell cmdlets to reliably mount and unmount ISO files from within a batch script. This is the only recommended method, as there is no cmd.exe equivalent for this task.
The Challenge: No Native cmd.exe Command
The cmd.exe command prompt has no built-in utility for mounting or unmounting disk images. This functionality is part of the Windows Storage Management system, which is most easily controlled through PowerShell. Attempting to do this with older, third-party command-line tools is unreliable and not recommended on modern systems.
The Core Commands (PowerShell): Mount-DiskImage and Dismount-DiskImage
PowerShell provides two simple, powerful cmdlets that are the definitive tools for this job.
Mount-DiskImage: Takes the path to an ISO file and mounts it.Dismount-DiskImage: Takes the path to an ISO file and unmounts it.
We can execute these directly from our batch script using a PowerShell one-liner.
Basic Example: Mounting an ISO File
This script will mount a specified ISO file. Windows will automatically assign it the next available drive letter.
@ECHO OFF
SET "ISO_FILE=C:\Users\Admin\Downloads\office_professional_2021.iso"
ECHO Mounting the ISO file...
powershell -Command "Mount-DiskImage -ImagePath '%ISO_FILE%'"
ECHO.
ECHO --- Mount command sent successfully ---
ECHO Please check 'This PC' for a new virtual drive.
How to Get the Drive Letter of the Mounted ISO
Just mounting the ISO isn't enough; your script needs to know which drive letter was assigned to it so it can access the files. We can get this by capturing the output of the Mount-DiskImage command.
@ECHO OFF
SET "ISO_FILE=C:\Installers\my_app.iso"
SET "DriveLetter="
ECHO Mounting '%ISO_FILE%' and capturing the drive letter...
FOR /F "tokens=*" %%D IN (
'powershell -Command "(Mount-DiskImage -ImagePath '%ISO_FILE%' -PassThru | Get-Volume).DriveLetter"'
) DO (
SET "DriveLetter=%%D"
)
IF NOT DEFINED DriveLetter (
ECHO [FAILURE] Could not mount the ISO or get the drive letter.
GOTO :EOF
)
ECHO.
ECHO [SUCCESS] ISO mounted to drive: %DriveLetter%:
DIR %DriveLetter%:
-PassThru: This crucial switch makesMount-DiskImageoutput an object representing the disk.| Get-Volume: We pipe that disk object toGet-Volume, which contains the drive letter information..DriveLetter: We then select only theDriveLetterproperty.
Unmounting the ISO File
Unmounting is just as important to clean up the virtual drive after your script is finished. You use the Dismount-DiskImage cmdlet.
@ECHO OFF
SET "ISO_FILE=C:\Installers\my_app.iso"
ECHO Unmounting the ISO file...
powershell -Command "Dismount-DiskImage -ImagePath '%ISO_FILE%'"
ECHO.
ECHO --- Dismount command sent ---
This will remove the virtual drive from "This PC."
Common Pitfalls and How to Solve Them
Problem: The Script is Not Run as Administrator
Mounting a disk image is a system-level operation that requires elevated privileges.
Example of error message (in PowerShell)
Mount-DiskImage : The requested operation requires elevation.
Solution: The script must be run as an Administrator. Right-click your .bat file or cmd.exe and select "Run as administrator."
Problem: The ISO is Already Mounted or the Drive Letter is in Use
Mount-DiskImage: If the ISO is already mounted, the command may fail or behave unexpectedly.Dismount-DiskImage: If the virtual drive is in use (e.g., you have a File Explorer window open to it), the dismount may fail.
Solution: A robust script should handle these states.
- Before Mounting: You can add
-ErrorAction SilentlyContinueto the PowerShell command to prevent it from showing an error if the disk is already mounted. - Before Unmounting: Ensure that any processes using the drive are closed.
Practical Example: A Silent Software Installer Script
This script automates the full cycle: it mounts an ISO containing a software installer, runs the setup file silently, and then unmounts the ISO when it's done.
@ECHO OFF
SETLOCAL
REM This script must be run as an Administrator.
SET "ISO_PATH=C:\Installers\business_app_v2.iso"
SET "SETUP_EXE=setup.exe"
SET "DriveLetter="
ECHO --- Automated ISO Installer ---
IF NOT EXIST "%ISO_PATH%" (ECHO [ERROR] ISO file not found. & GOTO :End)
ECHO Step 1: Mounting the ISO...
FOR /F "tokens=*" %%D IN ('powershell -NoProfile -ExecutionPolicy Bypass -Command "(Mount-DiskImage -ImagePath '%ISO_PATH%' -PassThru | Get-Volume).DriveLetter"') DO (
SET "DriveLetter=%%D"
)
IF NOT DEFINED DriveLetter (ECHO [ERROR] Failed to mount ISO. & GOTO :End)
ECHO [SUCCESS] ISO mounted to drive %DriveLetter%:
ECHO.
SET "InstallerPath=%DriveLetter%:\%SETUP_EXE%"
IF NOT EXIST "%InstallerPath%" (
ECHO [ERROR] Installer '%SETUP_EXE%' not found on the ISO.
GOTO :Cleanup
)
ECHO Step 2: Running the installer silently...
START /WAIT "" "%InstallerPath%" /silent /norestart
:Cleanup
ECHO.
ECHO Step 3: Unmounting the ISO...
powershell -NoProfile -ExecutionPolicy Bypass -Command "Dismount-DiskImage -ImagePath '%ISO_PATH%'"
:End
ECHO --- Installation script finished ---
ENDLOCAL
Conclusion
While cmd.exe cannot mount ISO files natively, it can easily control the powerful PowerShell cmdlets that are built into all modern versions of Windows.
Key takeaways:
- There is no pure-batch method; you must use PowerShell.
- Use
Mount-DiskImageto mount an ISO andDismount-DiskImageto unmount it. - You must run your script as an Administrator for these commands to work.
- To get the drive letter, use the
-PassThru | Get-Volumepipeline and capture the output with aFOR /Floop.