Functions/GenXdev.Software/EnsureWindowsMediaFeaturePack.ps1

<##############################################################################
Part of PowerShell module : GenXdev.Software
Original cmdlet filename : EnsureWindowsMediaFeaturePack.ps1
Original author : René Vaessen / GenXdev
Version : 3.28.2026
################################################################################
Copyright (c) 2026 René Vaessen / GenXdev

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
################################################################################>


function EnsureWindowsMediaFeaturePack {

    [CmdletBinding()]
    param(
        [switch] $Force,

        ########################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ('Automatically consent to Windows Media Feature ' +
                'Pack and Edge installation and set persistent flag.')
        )]
        [switch] $AutoConsent,
        ########################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ('Automatically consent to third-party software ' +
                'installation and set persistent flag for all packages.')
        )]
        [switch] $AutoConsentAllPackages,
        ########################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ('Only auto consent during session')
        )]
        [switch]$SessionOnly
        ########################################################################
    )

    begin {
        if ($Force) {
            Microsoft.PowerShell.Utility\Write-Verbose "Force mode enabled – will ensure mfplat.dll exists."
        }
    }

    process {

        $edition = (CimCmdlets\Get-CimInstance Win32_OperatingSystem).Caption
        $isN = $edition -like '*N*'

        if ($isN) {

            Microsoft.PowerShell.Utility\Write-Verbose "Detected Windows N edition – installing Media Feature Pack."

            $cap = Dism\Get-WindowsCapability -Online -Name 'Media.MediaFeaturePack~~~~0.0.1.0' | Microsoft.PowerShell.Core\Where-Object State -eq Installed

            if (-not $cap) {

                # YOUR CONSENT PROMPT — preserved exactly
                $consent = GenXdev\Confirm-InstallationConsent `
                    -ApplicationName 'Windows Media Feature Pack' `
                    -Source 'Windows' `
                    -Description 'Your $Edition is missing critical components for playing video, text-to-speech, etc' `
                    -Publisher 'Microsoft' `
                    -AutoConsent:$AutoConsent `
                    -AutoConsentAllPackages:$AutoConsentAllPackages

                if (-not $consent) {
                    Microsoft.PowerShell.Utility\Write-Verbose 'Windows Media Feature Pack installation cancelled by user.'
                    return
                }

                Microsoft.PowerShell.Utility\Write-Verbose "Media Feature Pack not installed – installing now."

                try {
                    Dism\Add-WindowsCapability -Online -Name 'Media.MediaFeaturePack~~~~0.0.1.0' -ErrorAction Stop

                    # verify the capability was installed
                    $cap = Dism\Get-WindowsCapability -Online `
                        -Name 'Media.MediaFeaturePack~~~~0.0.1.0' |
                        Microsoft.PowerShell.Core\Where-Object State -eq Installed
                    if (-not $cap) {
                        throw 'Media Feature Pack installation verification failed — capability not found after installation.'
                    }
                }
                catch {
                    throw "Failed to install Media Feature Pack: $($_.Exception.Message)"
                }
            }
        }
        else {
            Microsoft.PowerShell.Utility\Write-Verbose "Non‑N edition – Media Feature Pack is irrelevant."
        }

        if ($Force) {

            $edge = Microsoft.WinGet.Client\Get-WinGetPackage -Id Microsoft.Edge | Microsoft.PowerShell.Core\Where-Object { $_.Id -eq 'Microsoft.Edge' }

            if ($edge) {

                Microsoft.PowerShell.Utility\Write-Verbose "Microsoft Edge is already installed."
                return
            }

            Microsoft.PowerShell.Utility\Write-Warning "Microsoft Edge is not installed – but needed for certain media features like text-to-speech."

            if ($fod) {

                $consent = GenXdev\Confirm-InstallationConsent `
                    -ApplicationName 'Microsoft Edge Browser (AppX)' `
                    -Source 'Windows' `
                    -Description 'Installing Edge from offline FoD package.' `
                    -Publisher 'Microsoft' `
                    -AutoConsent:$AutoConsent `
                    -AutoConsentAllPackages:$AutoConsentAllPackages

                if (-not $consent) {

                    Microsoft.PowerShell.Utility\Write-Warning "Edge installation cancelled by user."
                    return
                }

                try {
                    Microsoft.WinGet.Client\Install-WinGetPackage -Id Microsoft.Edge -Force

                    Microsoft.PowerShell.Utility\Write-Host "Microsoft Edge installed from offline package."
                    return
                }
                catch {

                }
            }
        }
    }
}