PowerShell Module Installation
Return to PowerShell, PowerShell Topics
Installing PowerShell modules is a common task for extending the capabilities of PowerShell. Modules can be installed from the PowerShell Gallery or other repositories. Here’s a comprehensive guide on how to install PowerShell modules.
- Steps to Install a PowerShell Module
- 1. Open PowerShell as an Administrator
To install modules system-wide, you need administrative privileges. Right-click on PowerShell and select “Run as administrator.”
- 2. Verify PowerShell Version
Ensure you have a compatible version of PowerShell by checking your PowerShell version: ```powershell $PSVersionTable.PSVersion ```
- 3. Register the PowerShell Gallery Repository
Ensure that the PowerShell Gallery is registered as a repository: ```powershell Get-PSRepository ``` If the PowerShell Gallery is not listed, register it: ```powershell Register-PSRepository -Default ```
- 4. Install the Module
Use the `Install-Module` cmdlet to install the module. For example, to install the `AzureAD` module: ```powershell Install-Module -Name AzureAD ```
If you encounter a prompt about an untrusted repository, confirm the installation by responding with `Y` (Yes) or `A` (Yes to All).
- 5. Import the Module
After installation, import the module into your current session: ```powershell Import-Module AzureAD ```
- 6. Verify Installation
Ensure that the module is installed and loaded by listing the available cmdlets: ```powershell Get-Command -Module AzureAD ```
- Handling Common Issues
- Untrusted Repository
If you see a warning about an untrusted repository, you can set the PowerShell Gallery as a trusted repository: ```powershell Set-PSRepository -Name PSGallery -InstallationPolicy Trusted ```
- Updating PowerShellGet and PackageManagement
If you encounter issues, ensure that you have the latest versions of `PowerShellGet` and `PackageManagement`: ```powershell Install-Module -Name PowerShellGet -Force -AllowClobber Install-Module -Name PackageManagement -Force -AllowClobber ```
- Running PowerShell as Administrator
Ensure you are running PowerShell with administrative privileges to install modules system-wide.
- Example: Installing the AzureAD Module
Here is the complete sequence of commands to install the `AzureAD` module:
```powershell
- Open PowerShell as an Administrator
- Check PowerShell version
$PSVersionTable.PSVersion
- Check the registered repositories
Get-PSRepository
- Register the PowerShell Gallery repository if not listed
Register-PSRepository -Default
- Install the AzureAD module
Install-Module -Name AzureAD
- Import the AzureAD module
Import-Module AzureAD
- Verify the installation
Get-Command -Module AzureAD ```
- Example: Installing Multiple Modules
You can install multiple modules in a single script. Here is an example:
```powershell
- Modules to install
$modules = @(“AzureAD”, “Az”, “PSReadline”)
foreach ($module in $modules) {
Install-Module -Name $module -Force -AllowClobber Import-Module $module}
- Verify installations
$modules ]] | [[ ForEach-Object { Get-Command -Module $_ } ```
- Installing from a Custom Repository
If you need to install a module from a custom repository, register the repository first:
```powershell
- Register a custom repository
Register-PSRepository -Name CustomRepo -SourceLocation “https://customrepo.example.com/nuget/v2” -InstallationPolicy Trusted
- Install a module from the custom repository
Install-Module -Name CustomModule -Repository CustomRepo ```
- Updating and Uninstalling Modules
- Updating a Module
To update a module to the latest version: ```powershell Update-Module -Name AzureAD ```
- Uninstalling a Module
To uninstall a module: ```powershell Uninstall-Module -Name AzureAD ```
By following these steps, you can effectively manage PowerShell modules, ensuring that you have the necessary tools and extensions for your automation and management tasks.