120

To check if a module exists I have tried the following:

try {
    Import-Module SomeModule
    Write-Host "Module exists"
} 
catch {
    Write-Host "Module does not exist"
}

The output is:

Import-Module : The specified module 'SomeModule' was not loaded because no valid module file was found in any module directory.
At D:\keytalk\Software\Client\TestProjects\Export\test.ps1:2 char:5
+     Import-Module SomeModule
+     ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (SomeModule:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

Module exists

I do get an error, but no exception is thrown, so we see Module exists in the end, although SomeModule does not exist.

Is there a good way (preferably without generating an error) to detect if a PowerShell module is installed on the system?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Klemens Schindler
  • 2,949
  • 3
  • 10
  • 10

14 Answers14

157

You can use the ListAvailable option of Get-Module:

if (Get-Module -ListAvailable -Name SomeModule) {
    Write-Host "Module exists"
} 
else {
    Write-Host "Module does not exist"
}
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Klemens Schindler
  • 2,949
  • 3
  • 10
  • 10
  • 1
    I was going to suggets: Import-Module NonexistingModule -ErrorAction SilentlyContinue IF($error){Write-Host 'Module does not exist'} ELSE{Write-Host 'Module does exist'} But your way is better, more elegant :) – Erik Blomgren Feb 26 '15 at 10:58
  • This works great. Thanks. I'll use `Write-Warning "Module does not exist..." ;Break` But you've done all the hard work. – Craig.C Dec 20 '16 at 20:25
  • If you are importing libraries using `Import-Module` and a custom dll file, don't use the `-ListAvailable` option to determine if the module is installed because it won't be listed. According to [the PowerShell 6 documentation](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-module?view=powershell-6#parameters), "ListAvailable does not return information about modules that are not found in the PSModulePath environment variable, even if those modules are loaded in the current session". – Dave F Sep 02 '19 at 13:50
  • This does not determine if a module has been installed (i.e. `Import-Module`) it only determines if the module is immediately available to be installed without specifying a specific location not already in `$env:PSModulePath` – Slogmeister Extraordinaire May 21 '20 at 18:17
41

A module could be in the following states:

  • imported
  • available on disk (or local network)
  • available in an online gallery

If you just want to have the darn thing available in a PowerShell session for use, here is a function that will do that or exit out if it cannot get it done:

function Load-Module ($m) {

    # If module is imported say that and do nothing
    if (Get-Module | Where-Object {$_.Name -eq $m}) {
        write-host "Module $m is already imported."
    }
    else {

        # If module is not imported, but available on disk then import
        if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
            Import-Module $m -Verbose
        }
        else {

            # If module is not imported, not available on disk, but is in online gallery then install and import
            if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
                Install-Module -Name $m -Force -Verbose -Scope CurrentUser
                Import-Module $m -Verbose
            }
            else {

                # If the module is not imported, not available and not in the online gallery then abort
                write-host "Module $m not imported, not available and not in an online gallery, exiting."
                EXIT 1
            }
        }
    }
}

Load-Module "ModuleName" # Use "PoshRSJob" to test it out
Federico Navarrete
  • 2,641
  • 5
  • 39
  • 64
Rod
  • 1,283
  • 13
  • 17
36

The ListAvailable option doesn't work for me. Instead this does:

if (-not (Get-Module -Name "<moduleNameHere>")) {
    # module is not loaded
}

Or, to be more succinct:

if (!(Get-Module "<moduleNameHere>")) {
    # module is not loaded
}
GaTechThomas
  • 4,746
  • 3
  • 38
  • 61
karezza
  • 586
  • 5
  • 12
  • @oɔɯǝɹ I thought -ListAvailable was simply not available but I was still trying Import-Module. With Get-Module it's fine – Craig.C Dec 20 '16 at 20:16
  • 4
    You check if module LOADED(which is useful by itself -http://www.systemcentercentral.com/powershell-tip-how-to-load-if-a-module-is-loaded-before-attempting-import-module-3/), but not the other answer checks if module exists. – Michael Freidgeim Jan 10 '17 at 22:31
  • 1
    This executes much faster than using ListAvailable. – GaTechThomas Apr 21 '17 at 19:33
  • Fairly sure `!` doesn't work in powershell depending on the version? – Kellen Stuart Jun 20 '17 at 17:46
  • 2
    @KolobCanyon `!` is an alias for `-not`, but I would not recommend using aliases in ps1 scripts in general. @GaTechThomas it also has a different behavior, as specified by @MichaelFreidgeim (it does not return a truthy value for installed, but not imported, modules). – AndreasHassing Jan 22 '18 at 11:00
  • I am unable to use this script via Core; the accepted answer that utilizes "-ListAvailable" works like a charm, however. – CodeBreaker Nov 16 '20 at 21:58
24

The current version of Powershell has a Get-InstalledModule function that suits this purpose well (or at least it did in my case).

Get-InstalledModule

Description

The Get-InstalledModule cmdlet gets PowerShell modules that are installed on a computer.

The only issue with it is that it throws an exception if the module that is being requested doesn't exist, so we need to set ErrorAction appropriately to suppress that case.

if ((Get-InstalledModule `
    -Name "AzureRm.Profile" `
    -MinimumVersion 5.0 ` # Optionally specify minimum version to have
    -ErrorAction SilentlyContinue) -eq $null) {

    # Install it...
}
NightOwl888
  • 53,678
  • 21
  • 130
  • 204
  • `Get-InstalledModule` only accounts for modules installed via `Install-Module`, `Get-Module -List` will account for all available modules regardless of how they were installed – gordy Apr 29 '22 at 17:51
16

You can use the Get-InstalledModule

If (-not(Get-InstalledModule SomeModule -ErrorAction silentlycontinue)) {
  Write-Host "Module does not exist"
}
Else {
  Write-Host "Module exists"
}
mellifluous
  • 1,535
  • 18
  • 40
  • 2
    Many good answers here, but with this new simple method, this should probably be the new accepted answer. – not2qubit Sep 29 '20 at 10:55
  • This only works if `PowerShellGet` is installed. On some airgapped servers this is not the case. – Diti Apr 20 '22 at 07:10
15

Just revisiting this as it's something I just faced and there is some incorrect stuff in the answers (though it's mentioned in the comments).

First thing though. The original questions ask how to tell if a PowerShell module is installed. We need to talk about the word installed! You don't install PowerShell modules (not in the traditional way you install software anyway).

PowerShell modules are either available (i.e. they are on the PowerShell module path), or they are imported (they are imported into your session and you can call the functions contained). This is how to check your module path, in case you want to know where to store a module:

$env:psmodulepath

I'd argue that it's becoming common to use C:\Program Files\WindowsPowerShell\Modules; more often due to it being available to all users, but if you want to lock down your modules to your own session, include them in your profile. C:\Users%username%\Documents\WindowsPowerShell\Modules;

Alright, back to the two states.

Is the module available (using available to mean installed in the original question)?

Get-Module -Listavailable -Name <modulename>

This tells you if a module is available for import.

Is the module imported? (I'm using this as the answer for the word 'exists' in the original question).

Get-module -Name <modulename>

This will either return an empty load of nothing if the module is not imported or a one-line description of the module if it is. As ever on Stack  Overflow, try the commands above on your own modules.

Federico Navarrete
  • 2,641
  • 5
  • 39
  • 64
bytejunkie
  • 963
  • 11
  • 29
  • 1
    You can install module in PowerShell. PowerShellGet has command `Get-InstalledModule` which is not returning the same output as `Get-Module -ListAvailable` – Igor May 23 '19 at 19:30
14

When I use non-default modules in my scripts I call the function below. Besides the module name, you can provide a minimum version.

# See https://www.powershellgallery.com/ for module and version info
Function Install-ModuleIfNotInstalled(
    [string] [Parameter(Mandatory = $true)] $moduleName,
    [string] $minimalVersion
) {
    $module = Get-Module -Name $moduleName -ListAvailable |`
        Where-Object { $null -eq $minimalVersion -or $minimalVersion -lt $_.Version } |`
        Select-Object -Last 1
    if ($null -ne $module) {
         Write-Verbose ('Module {0} (v{1}) is available.' -f $moduleName, $module.Version)
    }
    else {
        Import-Module -Name 'PowershellGet'
        $installedModule = Get-InstalledModule -Name $moduleName -ErrorAction SilentlyContinue
        if ($null -ne $installedModule) {
            Write-Verbose ('Module [{0}] (v {1}) is installed.' -f $moduleName, $installedModule.Version)
        }
        if ($null -eq $installedModule -or ($null -ne $minimalVersion -and $installedModule.Version -lt $minimalVersion)) {
            Write-Verbose ('Module {0} min.vers {1}: not installed; check if nuget v2.8.5.201 or later is installed.' -f $moduleName, $minimalVersion)
            #First check if package provider NuGet is installed. Incase an older version is installed the required version is installed explicitly
            if ((Get-PackageProvider -Name NuGet -Force).Version -lt '2.8.5.201') {
                Write-Warning ('Module {0} min.vers {1}: Install nuget!' -f $moduleName, $minimalVersion)
                Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Scope CurrentUser -Force
            }        
            $optionalArgs = New-Object -TypeName Hashtable
            if ($null -ne $minimalVersion) {
                $optionalArgs['RequiredVersion'] = $minimalVersion
            }  
            Write-Warning ('Install module {0} (version [{1}]) within scope of the current user.' -f $moduleName, $minimalVersion)
            Install-Module -Name $moduleName @optionalArgs -Scope CurrentUser -Force -Verbose
        } 
    }
}

usage example:

Install-ModuleIfNotInstalled 'CosmosDB' '2.1.3.528'

Please let me known if it's useful (or not)

Federico Navarrete
  • 2,641
  • 5
  • 39
  • 64
TJ Galama
  • 409
  • 3
  • 10
7
try {
    Import-Module SomeModule
    Write-Host "Module exists"
} 
catch {
    Write-Host "Module does not exist"
}

It should be pointed out that your cmdlet Import-Module has no terminating error, therefore the exception isn't being caught so no matter what your catch statement will never return the new statement you have written.

(https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-6

From The Above:

"A terminating error stops a statement from running. If PowerShell does not handle a terminating error in some way, PowerShell also stops running the function or script using the current pipeline. In other languages, such as C#, terminating errors are referred to as exceptions. For more information about errors, see about_Errors."

It should be written as:

Try {
    Import-Module SomeModule -Force -Erroraction stop
    Write-Host "yep"
}
Catch {
    Write-Host "nope"
}

Which returns:

nope

And if you really wanted to be thorough you should add in the other suggested cmdlets Get-Module -ListAvailable -Name and Get-Module -Name to be extra cautious, before running other functions/cmdlets. And if it's installed from ps gallery or elsewhere you could also run a Find-Module cmdlet to see if there is a new version available.

Federico Navarrete
  • 2,641
  • 5
  • 39
  • 64
mwtilton
  • 71
  • 1
  • 2
6

You can use the #Requires statement (supports modules from PowerShell 3.0).

The #Requires statement prevents a script from running unless the PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met.

So At the top of the script, simply add #Requires -Module <ModuleName>

If the required modules are not in the current session, PowerShell imports them.

If the modules cannot be imported, PowerShell throws a terminating error.

sheldonzy
  • 4,613
  • 9
  • 43
  • 74
  • 1
    Note for others that might think this is such a cleaner solution than the longer functions above: this will *import* the module if it is not in the current session, but it will not go out and install it from internet sources. So you may need/require the longer solutions if you're going to be deploying this to machines that may or may not already have the module installed. – Adam Nofsinger Apr 15 '21 at 16:42
4

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)."
}
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Eddie Kumar
  • 893
  • 12
  • 16
1
  • First test if the module is loaded
  • Then import

```

if (Get-Module -ListAvailable -Name <<MODULE_NAME>>) {
    Write-Verbose -Message "<<MODULE_NAME>> Module does not exist." -Verbose
}
if (!(Get-Module -Name <<MODULE_NAME>>)) {
    Get-Module -ListAvailable <<MODULE_NAME>> | Import-Module | Out-Null
}

```

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
1

Coming from Linux background. I would prefer using something similar to grep, therefore I use Select-String. So even if someone is not sure of the complete module name. They can provide the initials and determine whether the module exists or not.

Get-Module -ListAvailable -All | Select-String Module_Name(can be a part of the module name)

010 M
  • 51
  • 3
1

Here is the code to check if AZ module is installed or not:

$checkModule = "AZ"

$Installedmodules = Get-InstalledModule

if ($Installedmodules.name -contains $checkModule)
{

    "$checkModule is installed "

}

else {

    "$checkModule is not installed"

}
Alkum
  • 11
  • 2
0

The absolute simplest one-liner without if-else block using Az module as an example:

Get-InstalledModule Az

This is what you want if you're working in the shell console and just want to check if a PowerShell module is installed or not.

Samuel
  • 2,665
  • 3
  • 29
  • 41