Private/Test/Test-IsModuleAvailable.ps1

function Test-IsModuleAvailable {
    <#
        .SYNOPSIS
        Tests if a PowerShell module is available for import on the system.
 
        .DESCRIPTION
        Checks if the specified PowerShell module is installed and available on the system.
        This searches the module paths for installed modules that can be imported.
        This does not check if the module is currently loaded in the session.
         
        Use this function to determine if a module is installed before attempting to import it.
 
        .PARAMETER Name
        The name of the module to check. This is case-insensitive and supports
        wildcards if needed.
 
        .INPUTS
        System.String
        You can pipe a module name to this function.
 
        .OUTPUTS
        System.Boolean
        Returns $true if the module is installed and available on the system.
        Returns $false if the module is not available.
 
        .EXAMPLE
        Test-IsModuleAvailable -Name 'ActiveDirectory'
        Returns $true if the ActiveDirectory module is installed and available.
 
        .EXAMPLE
        if (-not (Test-IsModuleAvailable -Name 'Pester')) {
            Install-Module Pester -Scope CurrentUser
        }
        Installs the Pester module only if it's not already available on the system.
 
        .EXAMPLE
        $modules = @('ActiveDirectory', 'GroupPolicy', 'DnsServer')
        $modules | ForEach-Object {
            if (Test-IsModuleAvailable -Name $_) {
                Write-Host "$_ is available"
            }
        }
        Checks multiple modules to see which are installed and available.
 
        .NOTES
        This function checks only available (installed) modules, not loaded modules.
        To check if a module is currently loaded in the session,
        use Get-Module -Name <ModuleName> without -ListAvailable.
 
        .LINK
        https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-module
    #>

    [CmdletBinding()]
    [OutputType([bool])]
    param (
        [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [string]$Name
    )

    #requires -Version 5.1

    process {
        Write-Verbose "Checking if module '$Name' is available (installed) on the system"
        
        try {
            # Check if the module is available to be loaded in the session
            $result = [bool](Get-Module -Name $Name -ListAvailable -ErrorAction Stop)
            Write-Verbose "Module '$Name' availability: $result"
            $result
        } catch {
            $errorRecord = [System.Management.Automation.ErrorRecord]::new(
                $_.Exception,
                'ModuleAvailabilityCheckFailed',
                [System.Management.Automation.ErrorCategory]::NotSpecified,
                $Name
            )
            $PSCmdlet.WriteError($errorRecord)
            return $false
        }
    }
}