Public/Get-specModule.ps1

Function Get-specModule {
    <#
    .SYNOPSIS
        Checks if one or more specified PowerShell modules are installed and optionally filters by version.
 
    .DESCRIPTION
        The Get-specModule function determines if a given set of PowerShell modules are currently installed on your system.
        It supports single module names, multiple module names as an array, and pipeline input. The function can optionally
        check for the presence of a specific version of a module.
 
    .PARAMETER moduleName
        One or more PowerShell module names. Module names can be provided as:
           * A single string
           * An array of strings
           * Pipeline input
 
    .PARAMETER version
        If specified, the function will attempt to find a module matching the given version number.
 
    .EXAMPLE
        Get-specModule -moduleName Az.Accounts
 
        Checks if the 'Az.Accounts' module is installed (any version).
 
    .EXAMPLE
        Get-specModule -moduleName DHCPServer -version 1.0.5
 
        Checks if version 1.0.5 of the 'DHCPServer' module is installed.
 
    .EXAMPLE
        "AzureAD", "ExchangeOnlineManagement" | Get-specModule
 
        Uses pipeline input to check if 'AzureAD' and 'ExchangeOnlineManagement' modules are installed.
 
    .NOTES
        * 1.0.0 - owen.heaume - Initial release
    #>


    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]$ModuleName,

        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string]$Version
    )

    Begin {
        $result = @()
    }

    Process {
        foreach ($module in $ModuleName) {
            try {
                Write-Host "Checking for installed module: '$module' $Version" -ForegroundColor DarkCyan
                $availableVersions = Get-Module -Name $module -ListAvailable

                if ($version) {
                    # Filter for the requested version
                    $moduleFound = $availableVersions | Where-Object { $_.Version.ToString() -eq $Version }
                } else {
                    # No version specified - take any available version
                    $moduleFound = $availableVersions
                }

                if ($moduleFound) {
                    Write-Host "'$module' is already installed`n" -ForegroundColor DarkGray
                    $result += [pscustomobject]@{
                        ModuleName  = $moduleFound.Name
                        Version     = $moduleFound.Version
                        IsInstalled = $true
                    }
                } else {
                    Write-Host "$module is not installed`n" -ForegroundColor DarkYellow
                    $result += [pscustomobject]@{
                        ModuleName  = $($module)
                        Version     = $Version
                        IsInstalled = $false
                    }
                }
            } catch {
                Write-Error "An unexpected error occurred: $_"
            }
        }
    }

    End {
        $result
    }
}