Table of Contents
Using Get-Module
Cmdlet
Use Get-Module
with -Name
parameter to check if a module is installed in PowerShell.
1 2 3 |
Get-Module -ListAvailable -Name ImportExcel |
1 2 3 4 5 6 |
Directory: C:\Users\user\Documents\WindowsPowerShell\Modules ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Script 7.8.4 ImportExcel {Add-ConditionalFormatting, Add-ExcelChart, Add-ExcelDataV... |
The Get-Module
retrieves information about loaded modules and available modules that can be imported.
The -ListAvailable
parameter filters the results only to show available modules that can be imported but are not currently loaded. This parameter will only display the modules available in the default PowerShell module directories and not necessarily any additional custom directories.
The -Name
parameter filters the results only to show modules that match the given name. In this case, the name being used is ImportExcel
. It is helpful to determine if a module is installed and available for import before attempting to import it with the Import-Module
cmdlet.
Using Test-ModuleManifest
Cmdlet
Use Test-ModuleManifest
to check if the module manifest file is valid in PowerShell.
1 2 3 |
Test-ModuleManifest -Path C:\Users\user\Documents\WindowsPowerShell\Modules\ImportExcel\7.8.4\ImportExcel.psd1 |
1 2 3 4 5 6 |
Directory: C:\Users\user\Documents\WindowsPowerShell\Modules ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Script 7.8.4 ImportExcel {Add-ConditionalFormatting, Add-ExcelChart, Add-ExcelDataV... |
The Test-ModuleManifest
cmdlet is used to validate a module manifest file and to verify that it contains the required fields and values.
The -Path
parameter specifies the path to the module manifest file that needs to be tested. In this case, the path is C:\Users\user\Documents\WindowsPowerShell\Modules\ImportExcel\7.8.4\ImportExcel.psd1, which is the path to the manifest file for the ImportExcel
module. When the cmdlet is run, it will read the manifest file and perform validation tests.
Further reading:
Using Load-Module()
Function
Use the Load-Module()
function to check if a module is installed in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function Load-Module ($n) { if (Get-Module | Where-Object {$_.name -eq $n}) { Write-Host "Module $n is already imported." } else { if (Get-Module -ListAvailable | Where-Object {$_.name -eq $n}) { Import-Module $n -Verbose } else { if (Find-Module -Name $n | Where-Object {$_.name -eq $n}) { Install-Module -Name $n -Force -Verbose -Scope CurrentUser Import-Module $n -Verbose } else { Write-Host "Module $n not imported, not available and not in an online gallery, exiting." Exit 1 } } } } Load-Module "ImportExcel" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
VERBOSE: Loading module from path 'C:\Users\user\Documents\WindowsPowerShell\Modules\ImportExcel\7.8.4\ImportExcel.psd1'. VERBOSE: Loading 'Assembly' from path 'C:\Users\user\Documents\WindowsPowerShell\Modules\ImportExcel\7.8.4\EPPlus.dll'. VERBOSE: Loading 'Assembly' from path 'C:\Users\user\Documents\WindowsPowerShell\Modules\ImportExcel\7.8.4\EPPlus.dll'. VERBOSE: Populating RepositorySourceLocation property for module ImportExcel. VERBOSE: Loading module from path 'C:\Users\user\Documents\WindowsPowerShell\Modules\ImportExcel\7.8.4\ImportExcel.psm1'. VERBOSE: Exporting function 'ColorCompletion'. VERBOSE: Exporting function 'ListFonts'. VERBOSE: Exporting function 'NumberFormatCompletion'. VERBOSE: Exporting function 'WorksheetArgumentCompleter'. VERBOSE: Exporting function 'Invoke-ExcelReZipFile'. .... |
In the above code, the Load-Module
takes a single parameter, $n
, that specifies the module’s name to load. Next, the function checks whether the module is already imported, and if it is, it simply outputs a message indicating that fact. Finally, if the module still needs to be imported, the function attempts to import it.
To import the module, the function first checks whether it is available on disk using the Get-Module
cmdlet with the -ListAvailable
parameter. Then, if available, the function uses the Import-Module
cmdlet to import it, with the -Verbose
parameter to output detailed information about the import process.
If the module is unavailable on disk, the function checks whether it is available in an online gallery using the Find-Module
cmdlet. If it is available, the function installs it using the Install-Module
cmdlet, with the -Force
parameter to force installation without prompting, the -Verbose
parameter to output detailed information about the installation process, and the -Scope CurrentUser
parameter to install the module for the current user only.
The function then imports the module as before. If the module is unavailable on disk and not in an online gallery, the function outputs a message indicating that fact and exits with an error code of 1.
Finally, the function is called with the parameter "ImportExcel"
, which is the name of the module to be loaded. This could be replaced with the name of any other module that the script needs to use.
PowerShell users can use the Get-Module
and Test-ModuleManifest
cmdlets and Load-Module()
function to ensure that the modules they need are installed and ready to use in their scripts and interactive sessions.
That’s all about PowerShell Check if Module is Installed.