The key to mounting ISOs programmatically in PowerShell is the Mount-DiskImage
cmdlet from the Storage module. This native Windows command provides complete control over ISO mounting operations.
# Basic syntax
Mount-DiskImage -ImagePath "C:\path\to\image.iso"
To specify the drive letter during mounting, we need to combine Mount-DiskImage
with Get-Volume
and Set-Volume
:
# Mount ISO to specific drive letter
$mountResult = Mount-DiskImage -ImagePath "C:\Office2021.iso" -PassThru
$volume = Get-Volume -DiskImage $mountResult
Set-Volume -InputObject $volume -NewDriveLetter X
Here's a production-ready script for mounting Office ISOs:
function Mount-OfficeISO {
param(
[Parameter(Mandatory=$true)]
[string]$ISOPath,
[char]$DriveLetter = 'O'
)
try {
Write-Host "Mounting Office ISO from $ISOPath..."
$diskImage = Mount-DiskImage -ImagePath $ISOPath -PassThru -ErrorAction Stop
# Wait for volume to be ready
Start-Sleep -Seconds 2
$volume = Get-Volume -DiskImage $diskImage
if ($volume.DriveLetter) {
Write-Host "ISO already mounted to $($volume.DriveLetter):"
return $volume.DriveLetter + ":"
}
Set-Volume -InputObject $volume -NewDriveLetter $DriveLetter -ErrorAction Stop
Write-Host "Successfully mounted to ${DriveLetter}:"
return "${DriveLetter}:"
}
catch {
Write-Error "Failed to mount ISO: $_"
return $null
}
}
# Usage example
$officeDrive = Mount-OfficeISO -ISOPath "C:\Downloads\Office2019.iso" -DriveLetter X
if ($officeDrive) {
# Run installation commands
Start-Process "$officeDrive\setup.exe" -ArgumentList "/configure configuration.xml"
}
Proper cleanup is essential for automation scripts:
function Dismount-OfficeISO {
param(
[Parameter(Mandatory=$true)]
[string]$ISOPath
)
try {
Dismount-DiskImage -ImagePath $ISOPath -ErrorAction Stop
Write-Host "Successfully dismounted Office ISO"
return $true
}
catch {
Write-Error "Failed to dismount ISO: $_"
return $false
}
}
# Usage
Dismount-OfficeISO -ISOPath "C:\Downloads\Office2019.iso"
Consider these additional scenarios:
- Check if ISO is already mounted before attempting
- Verify drive letter availability
- Handle cases where mounting fails
- Add retry logic for busy systems
# Check available drive letters
function Get-AvailableDriveLetters {
$used = (Get-Volume).DriveLetter | Where-Object { $_ -ne $null }
$all = 67..90 | ForEach-Object { [char]$_ } # C-Z
$available = $all | Where-Object { $_ -notin $used }
return $available
}
When automating software deployments like MS Office via ISO images, precise control over drive letter assignment is crucial for reliable scripting. Here's the PowerShell approach:
# Mount ISO to specific drive letter
Mount-DiskImage -ImagePath "C:\Office2021.iso" -DriveLetter X
# Verify mounted status
Get-Volume -DriveLetter X | Select-Object DriveLetter, FileSystemLabel
# Typical Office installation command
Start-Process "X:\setup.exe" -ArgumentList "/configure configuration.xml" -Wait
# Dismount when complete
Dismount-DiskImage -ImagePath "C:\Office2021.iso"
When the requested drive letter is already in use, you'll need error handling:
try {
Mount-DiskImage -ImagePath "C:\Office2021.iso" -DriveLetter X -ErrorAction Stop
}
catch {
Write-Warning "Drive X: in use, attempting next available"
$iso = Mount-DiskImage -ImagePath "C:\Office2021.iso" -PassThru
$driveLetter = (Get-Volume -DiskImage $iso).DriveLetter
Write-Host "Mounted to $driveLetter instead"
}
For enterprise deployment scenarios, consider these enhancements:
# Mount with read-only preference (recommended for ISOs)
Mount-DiskImage -ImagePath "C:\Office2021.iso" -DriveLetter X -Access ReadOnly
# Mount multiple Office components
$isos = @("Office.iso","Project.iso","Visio.iso")
$isos | ForEach-Object {
$driveLetter = [char]([int]([char]'D') + $isos.IndexOf($_))
Mount-DiskImage -ImagePath $_ -DriveLetter $driveLetter
}
Always validate successful mounting before proceeding with installations:
$maxRetries = 3
$retryInterval = 5
do {
Mount-DiskImage -ImagePath "C:\Office2021.iso" -DriveLetter X
if (Test-Path "X:\setup.exe") { break }
Start-Sleep -Seconds $retryInterval
$maxRetries--
} while ($maxRetries -gt 0)
if (-not (Test-Path "X:\setup.exe")) {
throw "Failed to mount ISO after multiple attempts"
}