How to use the command Install-Module (with examples)
- Windows
- December 25, 2023
The Install-Module command is used to install PowerShell modules from PowerShell Gallery, NuGet, and other repositories. It provides a convenient way to manage and update modules in PowerShell. The command allows you to install a module, update it to the latest version, specify a specific version, or install a module from a specific repository.
Use case 1: Install a module or update it to the latest available version
Code:
Install-Module module
Motivation: This use case is useful when you want to install a module or update it to the latest available version. It ensures that you have the latest features and bug fixes of the module.
Explanation: The “Install-Module” command is followed by the name of the module you want to install or update. If the module is not already installed, it will be installed. If the module is already installed, it will be updated to the latest available version.
Example output: The module is successfully installed or updated to the latest version.
Use case 2: Install a module with a specific version
Code:
Install-Module module -RequiredVersion version
Motivation: This use case is useful when you need to install a specific version of a module for compatibility reasons or to ensure consistency across different environments.
Explanation: The “Install-Module” command is followed by the name of the module you want to install and the “-RequiredVersion” parameter followed by the specific version number of the module. This will install the module with the specified version.
Example output: The module is successfully installed with the specified version.
Use case 3: Install a module no earlier than a specific version
Code:
Install-Module module -MinimumVersion version
Motivation: This use case is useful when you want to install a module that is compatible with a specific minimum version. It ensures that the installed module meets the minimum requirements.
Explanation: The “Install-Module” command is followed by the name of the module you want to install and the “-MinimumVersion” parameter followed by the specific version number. This will install the module with a version no earlier than the specified version.
Example output: The module is successfully installed with a version that meets the minimum requirement.
Use case 4: Specify a range of supported versions (inclusive) of the required module
Code:
Install-Module module -MinimumVersion minimum_version -MaximumVersion maximum_version
Motivation: This use case is useful when you want to specify a range of supported versions for the module. It allows you to ensure that only compatible versions are installed.
Explanation: The “Install-Module” command is followed by the name of the module you want to install, the “-MinimumVersion” parameter followed by the minimum version number, and the “-MaximumVersion” parameter followed by the maximum version number. This will install a version of the module that falls within the specified range.
Example output: The module is successfully installed with a version that falls within the specified range.
Use case 5: Install module from a specific repository
Code:
Install-Module module -Repository repository
Motivation: This use case is useful when you want to install a module from a specific repository. It allows you to control the source of the module.
Explanation: The “Install-Module” command is followed by the name of the module you want to install and the “-Repository” parameter followed by the name of the repository from which you want to install the module. This will install the module from the specified repository.
Example output: The module is successfully installed from the specified repository.
Use case 6: Install module from specific repositories
Code:
Install-Module module -Repository repository1, repository2, ...
Motivation: This use case is useful when you want to install a module from multiple repositories. It allows you to search for and install the module from different sources.
Explanation: The “Install-Module” command is followed by the name of the module you want to install and the “-Repository” parameter followed by a comma-separated list of repository names. This will search for the module in the specified repositories in the order provided and install it from the first repository where it is found.
Example output: The module is successfully installed from one of the specified repositories.
Use case 7: Install the module for all/current user
Code:
Install-Module module -Scope AllUsers|CurrentUser
Motivation: This use case is useful when you want to install a module for all users or only for the current user. It allows you to control the scope of the installation.
Explanation: The “Install-Module” command is followed by the name of the module you want to install and the “-Scope” parameter followed by either “AllUsers” or “CurrentUser”. This will install the module for all users or only for the current user, depending on the specified scope.
Example output: The module is successfully installed for all users or only for the current user.
Use case 8: Perform a dry run to determine which modules will be installed, upgraded, or removed through Install-Module
Code:
Install-Module module -WhatIf
Motivation: This use case is useful when you want to preview the changes that will be made by the “Install-Module” command without actually performing the installation. It allows you to verify the expected outcome before proceeding.
Explanation: The “Install-Module” command is followed by the name of the module you want to install and the “-WhatIf” parameter. This will display a summary of the changes that would be made by the command without actually making them.
Example output: A summary of the changes that would be made by the “Install-Module” command is displayed, indicating which modules will be installed, upgraded, or removed.
Conclusion
The Install-Module command is a powerful tool for managing PowerShell modules. Whether you want to install a module, update it to the latest version, specify a specific version, or install from a specific repository, the Install-Module command provides the flexibility and control you need. By understanding and using the different options and parameters available, you can easily manage your modules with confidence.