PowerShell modules are an integral part of extending the functionality of PowerShell. They are collections of cmdlets, functions, workflows, providers, and scripts that can be easily shared and reused. In this article, we'll explore the basics of PowerShell module management, covering installation, listing, updating, importing, and filtering the module list.

1. Listing Installed Modules:

Before managing modules, it's useful to know which modules are already installed on your system. The Get-Module cmdlet with the -ListAvailable parameter allows you to view a list of modules available on your system.

# Display all available modules
Get-Module -ListAvailable

This command displays information about all available modules. You can filter the list using -Name parameter for more specific results.

# Display only modules with "Name" in their name
Get-Module -ListAvailable -Name '*Name*'

Replace *Name* with the keyword you want to filter.

2. Listing All Available Repositories:

To view information about all registered repositories, use the Get-PSRepository cmdlet.

Get-PSRepository

This command displays a list of registered repositories along with their names, sources, and other relevant information.

Managing repositories is not part of this article.

Use this if you need to know where the modules are coming from.

3. Installing a Module from PowerShell Gallery:

To install a module from the registered repositories, use the Install-Module cmdlet.

Install-Module -Name ModuleName

Replace ModuleName with the actual name of the module you want to install.

5. Updating a Module:

Keeping modules up-to-date is essential for utilizing the latest features and improvements. The Update-Module cmdlet simplifies this process.

Update-Module -Name ModuleName

This command fetches and installs the latest version of the specified module.

6. Uninstalling a Module:

If a module is no longer needed, you can uninstall it using the Uninstall-Module cmdlet.

Uninstall-Module -Name ModuleName

This removes the specified module from your system.

7. Importing a Module (Without -Force):

When importing a module without the -Force parameter, PowerShell checks for conflicts with existing modules before importing.

Import-Module -Name ModuleName

This is the default behavior, and PowerShell only imports the module if there are no conflicts.

8. Importing a Module with -Force Parameter:

When importing a module with the -Force parameter, PowerShell forcefully imports the module, even if there are conflicts with existing modules.

Import-Module -Name ModuleName -Force

This is useful when you want to ensure that the module is imported, regardless of any conflicts.

Note: Starting with PowerShell 3.0, module auto-loading is the preferred method. PowerShell automatically loads a module when you use a cmdlet or function from that module. However, if you need to explicitly import a module, Import-Module is available.

Conclusion:

PowerShell module management is a straightforward process that involves listing, installing, updating, and uninstalling modules. Additionally, importing modules allows you to make their functionality available in your PowerShell session. To explore available repositories, use the Get-PSRepository cmdlet. When importing a module, consider using the -Force parameter if you encounter conflicts, or import without it to perform conflict checks.

Happy scripting!