private/Get-MountedWindowsImageIndex.ps1

#Requires -Version 7.4

function Get-MountedWindowsImageIndex {
    <#
    .SYNOPSIS
        Gets Windows image indexes from mounted installation media
 
    .DESCRIPTION
        Requires administrator privileges and uses Find-MountedWindowsMedia and
        Get-WimIndexCache to enumerate indexes in mounted install.wim and install.esd files.
        Numeric architecture values are converted to architecture names and each result is
        projected to a fixed set of media and image properties. When GridView is specified,
        Out-GridView controls which rows are returned. A warning is written and no objects
        are returned when the process is not elevated.
 
    .PARAMETER GridView
        Specifies the Out-GridView selection mode. Valid values are Single and Multiple.
        When omitted, all discovered rows are returned without displaying a picker.
 
    .EXAMPLE
        PS> Get-MountedWindowsImageIndex -GridView 'Single'
 
        Displays the discovered image indexes and returns the single row selected by the
        user. Canceling the picker returns no objects.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        System.Management.Automation.PSCustomObject. Returns zero or more image index rows
        with MediaRoot, ImagePath, ImageIndex, ImageName, Architecture, Version, EditionId,
        Languages, and the additional selected Windows image metadata properties.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 0.1.0
        Date: 2026-08-28
 
        Dependencies:
          Module Functions: Find-MountedWindowsMedia, Get-WimIndexCache,
                            Test-IsAdministrator
          PowerShell Modules: Dism
          Commands: Out-GridView when GridView is specified
    #>

    [CmdletBinding()]
    param (
        [Parameter()]
        [ValidateSet('Single', 'Multiple')]
        [string]$GridView
    )

    if (-not (Test-IsAdministrator)) {
        Write-Warning "[$(Get-Date -Format s)] Must be run with Administrator privileges"
        return
    }

    $PSDriveWindowsImageFile = Find-MountedWindowsMedia
    if (-not $PSDriveWindowsImageFile) {
        return $null
    }

    $WindowsMediaImages = $PSDriveWindowsImageFile | ForEach-Object {
        $MediaRoot = $_.MediaRoot
        $ImageFilePath = $_.FullName
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] [INFO] WindowsImage: $ImageFilePath"

        # Use cache to avoid expensive DISM calls on repeated runs
        $CachedImages = Get-WimIndexCache -ImagePath $ImageFilePath
        foreach ($cachedImage in $CachedImages) {
            $cachedImage | Select-Object -Property @{Name = 'MediaRoot'; Expression = { $MediaRoot } }, *
        }
    }

    # Translate Architecture numeric codes to human-readable strings
    foreach ($Image in $WindowsMediaImages) {
        switch ($Image.Architecture) {
            '0'  { $Image.Architecture = 'x86' }
            '1'  { $Image.Architecture = 'MIPS' }
            '2'  { $Image.Architecture = 'Alpha' }
            '3'  { $Image.Architecture = 'PowerPC' }
            '5'  { $Image.Architecture = 'ARM' }
            '6'  { $Image.Architecture = 'ia64' }
            '9'  { $Image.Architecture = 'amd64' }
            '12' { $Image.Architecture = 'arm64' }
        }
    }

    $Results = $WindowsMediaImages | Select-Object -Property MediaRoot, ImagePath, ImageIndex,
        ImageName, Architecture, Version, EditionId, Languages,
        InstallationType, CreatedTime, ModifiedTime,
        DirectoryCount, FileCount, ImageDescription, ImageSize, ImageType, WIMBoot,
        ProductName, Hal, ProductSuite, ProductType,
        MajorVersion, MinorVersion, Build, SPBuild, SPLevel,
        ImageBootable, SystemRoot, DefaultLanguageIndex, LogPath, ScratchDirectory, LogLevel

    if ($GridView) {
        $Results = $Results | Out-GridView -Title 'Select a WindowsImage and press OK (Cancel to Exit)' -OutputMode $GridView
    }

    return $Results
}