Installing PowerShell module persistently for all users

Learn installing powershell module persistently for all users with practical examples, diagrams, and best practices. Covers powershell, module, powershell-module development techniques with visual ...

Installing PowerShell Modules Persistently for All Users

PowerShell logo with multiple user icons, symbolizing module availability for all users.

Learn how to install PowerShell modules so they are available to all users on a system, ensuring consistent access and functionality across different user profiles.

PowerShell modules extend the functionality of PowerShell, providing cmdlets, functions, and other tools for managing systems. When working in multi-user environments or setting up automation scripts that need to run under different user contexts, it's crucial to install modules in a location accessible to all users on the system. This article will guide you through the process of installing PowerShell modules persistently for all users, covering the default installation paths and best practices.

Understanding PowerShell Module Paths

PowerShell looks for modules in specific directories defined by the $env:PSModulePath environment variable. This variable contains a semicolon-separated list of paths. When you import a module, PowerShell scans these paths in order to find the module's manifest or script file. Understanding these paths is key to installing modules correctly for all users.

flowchart TD
    A[Start PowerShell] --> B{Check $env:PSModulePath}
    B --> C["User-specific paths (e.g., C:\Users\<User>\Documents\PowerShell\Modules)"]
    B --> D["AllUsers paths (e.g., C:\Program Files\PowerShell\Modules)"]
    B --> E["System paths (e.g., C:\Windows\System32\WindowsPowerShell\v1.0\Modules)"]
    C --> F{Module Found?}
    D --> F
    E --> F
    F -->|Yes| G[Import Module]
    F -->|No| H[Error: Module Not Found]

PowerShell Module Resolution Flow

By default, $env:PSModulePath includes paths for user-specific modules and system-wide modules. To make a module available to all users, it must be installed in one of the system-wide paths. The most common and recommended path for modules intended for all users is typically found under C:\Program Files\PowerShell\Modules for PowerShell Core or C:\Program Files\WindowsPowerShell\Modules for Windows PowerShell.

$env:PSModulePath

Displaying the current PowerShell module paths.

Installing Modules for All Users

The Install-Module cmdlet is the primary way to install modules from the PowerShell Gallery. To ensure a module is installed for all users, you must use the -Scope AllUsers parameter. This parameter directs PowerShell to install the module into a system-wide path, typically C:\Program Files\PowerShell\Modules (for PowerShell Core) or C:\Program Files\WindowsPowerShell\Modules (for Windows PowerShell).

# Example: Installing the 'Pester' module for all users
Install-Module -Name Pester -Scope AllUsers -Force

Installing a module for all users using Install-Module.

The -Force parameter is often used to suppress confirmation prompts and to overwrite existing versions of the module if they are already present. This is particularly useful in automation scripts.

Manual Installation to AllUsers Path

In some cases, you might need to install a module manually, for example, if it's not available in the PowerShell Gallery or if you're deploying a custom module. For manual installations, you simply need to copy the module's folder into one of the system-wide paths listed in $env:PSModulePath. The most common target is C:\Program Files\PowerShell\Modules or C:\Program Files\WindowsPowerShell\Modules.

1. Identify the Module Folder

Locate the root folder of the module you wish to install. This folder typically contains the module's .psd1 (module manifest) or .psm1 (module script) file.

2. Choose an AllUsers Path

Determine the appropriate system-wide module path. You can check $env:PSModulePath for options. A common path is C:\Program Files\PowerShell\Modules for PowerShell Core or C:\Program Files\WindowsPowerShell\Modules for Windows PowerShell.

3. Copy the Module

Copy the entire module folder into the chosen system-wide module path. This operation usually requires administrative privileges.

4. Verify Installation

Open a new PowerShell session (as a non-administrator if testing user access) and run Get-Module -ListAvailable. The newly installed module should appear in the list.

# Example: Manually copying a module
$SourceModulePath = "C:\Temp\MyCustomModule"
$DestinationModulePath = "$($env:ProgramFiles)\PowerShell\Modules"

# Ensure the destination path exists
if (-not (Test-Path $DestinationModulePath)) {
    New-Item -Path $DestinationModulePath -ItemType Directory -Force
}

Copy-Item -Path $SourceModulePath -Destination $DestinationModulePath -Recurse -Force

# Verify installation
Get-Module -ListAvailable | Where-Object { $_.Name -eq 'MyCustomModule' }

Manually installing a custom module to the AllUsers path.