Powershell function to test if a Powershell module is installed. The function will return $true
if the module is installed or $false
if it is not installed.
<#
.SYNOPSIS
Test if a module is installed or not.
.DESCRIPTION
This test will return $true or $false depending on if a module is installed and available or not.
.PARAMETER Name
The module name to test
#>
function Test-IsModuleInstalled {
[cmdletbinding()]
param (
# The module name to test
[Parameter(Mandatory=$true,HelpMessage="The module name to check if installed")]
[String]
$Name
)
# Try to get the module information
Write-Verbose -Message "Running Get-Module for $($Name)..."
try {
$Module = Get-Module -ListAvailable -Name $Name -ErrorAction Stop
}
catch {
# Exception running Get-Module
$Message = $_.Exception.message
Write-Error -Message 'Exception recieved while running Get-Module:'
Write-Error -Message $Message
return $False
}
# Check if an item was returned
if ( $Module ) {
if ( $Module.Version ) {
Write-Verbose -Message "Module $($Name) found, version $($Module.Version)"
} else {
Write-Verbose -Message "Module $($Name) found, no version information"
}
return $True
} else {
Write-Verbose -Message "Module $($Name) not found"
return $False
}
}
Test if module name SomeModule
is installed:
Test-IsModuleInstalled -Name SomeModule