Private/Get-SpecInstalledModule.ps1

function Get-SpecInstalledModule {
    [cmdletbinding()]
    param (
        [string]$module,

        [string]$version
    )

    $installed = $false

    # query system to see if module is installed (any version)
    $result = get-module -Name $module -ListAvailable | sort version -Descending

    # module not installed at all
    if ([string]::IsNullOrEmpty($result)) {
        write-verbose "Module [$module] is not installed on this system"
        return $false
    }

    # module installed, but no specific version requested.
    # so let's return the latest installed version
    if ([string]::IsNullOrEmpty($version)) {
        $installedVersion = ($result | Sort-Object Version -Descending | Select-Object -First 1).version
        write-verbose "The module [$module] has been found on this system. Returning the version number [$installedVersion]"
        return $installedVersion
    }

    # Module installed but a specific version has been requested
    # so let's check if any of the installed version(s) are the requested version
    if ($version) {
        foreach ($Ver in $result) {
            if ($Ver.version -eq $version) {
                write-verbose "The installed version of [$module] matches the requested version of [$version]"
                $installedVersion = $ver.Version
                $installed = $true
                break
            }
        }
    }

    if ($installed) {
        write-verbose "The installed version of [$module] is at the required level [$installedVersion] "
        return $installedVersion
    } else {
        write-verbose "Module [$module] requires installing"
        return $false
    }
}