IMHO, there is difference between checking if a module is:
1) installed, or 2) imported:
To check if installed:
Option 1: Using Get-Module
with -ListAvailable
parameter:
If(Get-Module -ListAvailable -Name "<ModuleName>"){'Module is installed'}
Else{'Module is NOT installed'}
Option 2: Using $error
object:
$error.clear()
Import-Module "<ModuleName>" -ErrorAction SilentlyContinue
If($error){Write-Host 'Module is NOT installed'}
Else{Write-Host 'Module is installed'}
To check if imported:
Using Get-Module
with -Name
parameter (which you can omit as it is default anyway):
if ((Get-Module -Name "<ModuleName>")) {
Write-Host "Module is already imported (i.e. its cmdlets are available to be used.)"
}
else {
Write-Warning "Module is NOT imported (must be installed before importing)."
}