Private/Get-SpecInstalledModule.ps1
function Get-SpecInstalledModule { <# .SYNOPSIS Checks if a specified PowerShell module is installed and returns its version number. .DESCRIPTION The Get-SpecInstalledModule function queries the system to check if the specified PowerShell module is already installed. If the module is found, the function returns its version number; otherwise, it returns False. This function utilizes the Get-InstalledModule cmdlet from the PowerShellGet module. .PARAMETER module Specifies the name of the PowerShell module to check for installation. .PARAMETER version Specifies the required version of the PowerShell module to check for installation. If this parameter is provided, the function checks for the specified version of the module; otherwise, it checks for any installed version of the module. .EXAMPLE Get-SpecInstalledModule -module "ExampleModule" -version "1.2.0" Returns the version number of the installed "ExampleModule" if it matches version "1.2.0". .EXAMPLE Get-SpecInstalledModule -module "AnotherModule" Returns the version number of the installed "AnotherModule" if any version is installed. .EXAMPLE Get-SpecInstalledModule -module "NonExistentModule" Returns "False" if the "NonExistentModule" is not installed. .NOTES Author: owen.heaume Version: - 1.0 initial function - 1.1 complete code refactor #> [cmdletbinding()] param ( [string]$module, [string]$version ) try { write-verbose "Querying system to see if module is installed..." if ($version -ne "") { $installedModule = Get-InstalledModule -Name $module -RequiredVersion $version -ea stop -ev x -Verbose:$false return $installedModule.version } else { $installedModule = Get-InstalledModule -Name $module -ea stop -ev x -Verbose:$false return $installedModule.version } } catch { return $false } } |