Finding and installing modules
PowerShell includes a module named PowerShellGet
, which can be used to register repositories and search for and install modules.
By default, PowerShellGet
searches the PowerShell Gallery.
What is the PowerShell Gallery?
The PowerShell Gallery is a Microsoft-run repository and distribution platform for PowerShell scripts and modules written by Microsoft or other users.
The PowerShell Gallery has parallels in other scripting languages, as shown in the following examples:
- Perl has cpan.org
- Python has PyPI
- Ruby has RubyGems
Support for the gallery is included by default in PowerShell 5 and above. For Windows PowerShell 3 and 4, PowerShellGet must be installed as described in Microsoft Docs: https://docs.microsoft.com/powershell/scripting/gallery/installing-psget.
The PowerShell Gallery may be searched using https://www.powershellgallery.com as shown in the following screenshot:
Figure 2.2: Searching the PowerShell Gallery
You can use the Find-Module
command to search the PowerShell Gallery, or any registered repository, instead of using the web page.
The Find-Module command
Find-Module
is used to search registered PowerShell repositories. Modules are identified by name, as shown in the following example:
Find-Module Carbon
Find-Module -Name Carbon
Find-Module -Name Azure*
You can use the Filter
parameter when the name alone is not sufficient to find an appropriate module. Supplying a value for the Filter
parameter is equivalent to using the search field in the PowerShell Gallery, it expands the search to include tags:
Find-Module -Filter IIS
The Find-Module
command cannot filter based on PowerShell edition, and the result of the search does not state which version the module might work with.
Once found, a module can be installed using the Install-Module
command.
The Install-Module command
The Install-Module
command installs modules from the PowerShell Gallery or any other configured repository. By default, Install-Module
adds modules to the path for AllUsers
, at C:\Program Files\PowerShell\Modules
on Windows and at /usr/local/share/powershell/Modules
on Ubuntu.
Access rights
Installing a module under the AllUsers
scope requires administrative access.
For example, the posh-git
module may be installed using either of the following two commands:
Find-Module -Name posh-git | Install-Module
Install-Module posh-git
Modules may be installed under a user-specific path ($home\Documents\WindowsPowerShell\Modules
) using the Scope
parameter:
Install-Module carbon -Scope CurrentUser
If the most recent version of a module is already installed, the command ends without providing feedback. If a newer version is available, it will be automatically installed alongside the original.
The Force
parameter may be used to reinstall a module:
Install-Module posh-git -Force
Force
may also be used to install a newer version of a module when the existing version was not installed from a PS repository, or when changing the scope a module is installed in.
The Install-Module
command does not provide an option to install modules under the $PSHOME
directory. The $PSHOME
directory is reserved for modules that are shipped with the PowerShell installer, or for Windows PowerShell, those that are shipped with the Windows operating system.
The Update-Module command
You can use the Update-Module
command to update any module installed using the Install-Module
command.
In both Windows PowerShell and PowerShell 7, Update-Module
attempts to update the specified module to the latest or specified version.
The Save-Module command
The Save-Module
command downloads the module from the PowerShell Gallery (or any other registered repository) to a path without installing it. This is useful if you save a module to an alternate module path, or if you intend to copy the module onto a system that cannot use Install-Module
.
The following example command downloads the Carbon
module into a Modules
directory in the root of the C:
drive:
Save-Module -Name Carbon -Path C:\Modules
Save-Module
will download the module and overwrite any previously saved version in the specified path. The command ignores other downloaded versions of the module.
Each of the preceding commands is part of PowerShellGet
2. PowerShellGet
3 is likely to be released in the coming months and adopts a slightly different approach to the commands.