LLI Wiki — My Digital Knowledge Base

PowerShell: module paths and updates

How PSModulePath works, importing modules from outside the path, install scopes and updating modules.

How PowerShell finds modules, where modules are installed, how to import a module that is not on the module path, and how to keep installed modules up to date.

Module search path

PowerShell looks for modules in the directories listed in the PSModulePath environment variable, a semicolon-delimited list (on Windows). PowerShell 7 and Windows PowerShell 5.1 have different lists:

"PowerShell 7 paths:", ($env:PSModulePath -split ';')
"Windows PowerShell 5.1 paths:", (powershell -command "$env:PSModulePath -split ';'")

Typical Windows PowerShell locations:

Scope Path
Current user $env:UserProfile\Documents\WindowsPowerShell\Modules
All users $env:ProgramFiles\WindowsPowerShell\Modules
System $env:SystemRoot\system32\WindowsPowerShell\v1.0\Modules

Modules in the system location ship with PowerShell and are available system-wide; they should not be modified. Other tools (for example SQL Server Management Studio, System Center Operations Manager or the Chef Development Kit) may add their own paths to PSModulePath.

Use a module that is not on the path

Temporarily add a directory to PSModulePath for the current session. TestModule must be a sub-folder of that directory:

$env:PSModulePath += ';C:\Path\To\Temporary\ModuleDirectory'
Import-Module TestModule

The change lasts only for the session. To keep the module available permanently, copy it to one of the existing module directories or add its parent directory to the PSModulePath environment variable.

You can also import a module by path:

# Must have a valid .psd1 manifest in the ModuleName folder
Import-Module C:\SomeFolder\ModuleName

# You can directly import .psm1 files, but don't do this if a module manifest is present
Import-Module C:\SomeFolder\OtherModules\SomeModule.psm1

UNC (network) paths can also be added to PSModulePath; scripts loaded from a remote location remain subject to the execution policy of the system.

Install location

By default Install-Module installs to the current user location. The -Scope parameter selects between the two supported locations:

# Current user (default when the scope is not specified)
Install-Module -Scope CurrentUser $moduleName

# All users (requires elevated permissions)
Install-Module -Scope AllUsers $moduleName

Software that ships its own PowerShell modules should preferably add a dedicated directory to PSModulePath rather than install into the user-managed locations. This protects the modules from being modified or removed by accident.

See the Stack Overflow answer on how PowerShell finds modules.

Update installed modules

Update every module installed from the PowerShell Gallery:

Get-InstalledModule | Update-Module

Only report the modules that have a newer version:

Get-InstalledModule | foreach { $b = (find-module $_.name).version ; if ($b -ne $_.version) { Write-host "$($_.name) has an update from $($_.version) to $b" } }

Update the Azure* modules (except AzureRM) and remove the old version:

Get-Module -Name azure* -ListAvailable |
  Where-Object -Property Name -ne 'AzureRM' |
  ForEach-Object {
    $currentVersion = [Version] $_.Version
    $newVersion = [Version] (Find-Module -Name $_.Name).Version
    if ($newVersion -gt $currentVersion) {
      Write-Host -Object "Updating $_ Module from $currentVersion to $newVersion"
      Update-Module -Name $_.Name -RequiredVersion $newVersion -Force
      Uninstall-Module -Name $_.Name -RequiredVersion $currentVersion -Force
    }
  }

Installation problems

If module installation fails on an older system, update PowerShellGet and the NuGet package provider:

Install-Module –Name PowerShellGet –Force -AllowClobber
Install-PackageProvider Nuget -Force

Example of an installation for the current user (VMware PowerCLI):

Install-Module –Name VMware.PowerCLI –Scope CurrentUser –AllowClobber

If the PowerShell Gallery connection fails, enable TLS 1.2, re-register the default repository and mark it as trusted:

[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
Register-PSRepository -Default -Verbose
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted

Notes

Mastodon LLIMastodon SocialFosstodon