Public/Search-PSModule.ps1

function Search-PSModule {
    <#
    .SYNOPSIS
    Searches for modules in the PowerShell Gallery.
    .DESCRIPTION
    Searches PSGallery (or another repository) for PowerShell modules.
    Supports both fuzzy and exact search modes, with configurable sorting.
    Use -AuthorSearch to search by module author instead of module name.
    .PARAMETER Query
    The search term to use.
    .PARAMETER MaxResults
    Maximum number of results to return. Default is 99.
    .PARAMETER Skip
    Number of results to skip (for pagination). Default is 0.
    .PARAMETER ExactSearch
    If specified, performs an exact name match instead of fuzzy search.
    .PARAMETER AuthorSearch
    If specified, searches by author name instead of module name.
    .PARAMETER TagSearch
    If specified, searches by module tag instead of module name.
    .PARAMETER SortBy
    Sort order: Relevance, Downloads, or LastUpdated.
    .PARAMETER Repository
    The repository to search. Default is PSGallery.
    .EXAMPLE
    Search-PSModule -Query "azure"
    .EXAMPLE
    Search-PSModule -Query "Pester" -ExactSearch -SortBy Downloads
    .EXAMPLE
    Search-PSModule -Query "Microsoft" -AuthorSearch
    .EXAMPLE
    Search-PSModule -Query "PSEdition_Core" -TagSearch
    .EXAMPLE
    Search-PSModule -Query "azure" -Skip 99 -MaxResults 99
    #>

    [CmdletBinding()]
    [OutputType('GalleryModuleInfo[]')]
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$Query,

        [ValidateRange(1, [int]::MaxValue)]
        [int]$MaxResults = 99,

        [ValidateRange(0, [int]::MaxValue)]
        [int]$Skip = 0,

        [switch]$ExactSearch,

        [switch]$AuthorSearch,

        [switch]$TagSearch,

        [ValidateSet('Relevance', 'Downloads', 'LastUpdated')]
        [string]$SortBy = 'Relevance',

        [string]$Repository = 'PSGallery'
    )

    try {
        Write-PSMBLog -Message "Search-PSModule: query='$Query' skip=$Skip max=$MaxResults exact=$($ExactSearch.IsPresent) author=$($AuthorSearch.IsPresent) tag=$($TagSearch.IsPresent) sort=$SortBy repo=$Repository" -Level 'DEBUG'
        $results = Invoke-ProviderSearch -Query $Query -MaxResults $MaxResults -Skip $Skip -ExactSearch $ExactSearch.IsPresent -AuthorSearch $AuthorSearch.IsPresent -TagSearch $TagSearch.IsPresent -SortBy $SortBy -Repository $Repository
        return $results
    } catch {
        $ErrorActionPreference = 'Continue'
        Write-Error "Search-PSModule failed: $_"
        return @()
    }
}