Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Short description
This article describes the purpose and usage of the $Env:PSModulePath
environment variable.
Long description
The $Env:PSModulePath environment variable contains a list of folder
locations. PowerShell recursively searches each folder for module (.psd1 or
.psm1) files.
By default, the effective locations assigned to $Env:PSModulePath are:
- Modules installed in the CurrentUser scope are stored in
$HOME\Documents\WindowsPowerShell\Modules. - Modules installed in the AllUsers scope are stored in
$Env:ProgramFiles\WindowsPowerShell\Modules. - Modules that ship with Windows PowerShell stored in
$PSHOME\Modules, which is$Env:SystemRoot\System32\WindowsPowerShell\1.0\Modules.
PowerShell PSModulePath construction
The value of $Env:PSModulePath is constructed each time PowerShell starts.
The value varies by version of PowerShell and how you launched it.
Windows PowerShell startup
Windows PowerShell uses the following logic to construct the PSModulePath at
startup:
- If
PSModulePathdoesn't exist, combine CurrentUser, AllUsers, and the$PSHOMEmodules paths - If
PSModulePathdoes exist:- If
PSModulePathcontains$PSHOMEmodules path:- AllUsers modules path is inserted before
$PSHOMEmodules path
- AllUsers modules path is inserted before
- else:
- Just use
PSModulePathas defined since the user deliberately removed the$PSHOMElocation
- Just use
- If
The CurrentUser module path is prefixed only if the User scope
$Env:PSModulePath doesn't exist. Otherwise, the User scope
$Env:PSModulePath is used as defined.
Module search behavior
PowerShell recursively searches each folder in the PSModulePath for module
(.psd1 or .psm1) files. This search pattern allows multiple versions of the
same module to be installed in different folders. For example:
Directory: C:\Program Files\WindowsPowerShell\Modules\PowerShellGet
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 8/14/2020 5:56 PM 1.0.0.1
d---- 9/13/2019 3:53 PM 2.1.2
By default, PowerShell loads the highest version number of a module when
multiple versions are found. To load a specific version, use Import-Module
with the FullyQualifiedName parameter. For more information, see
Import-Module.
Modifying PSModulePath
For most situations, you should be installing modules in the default module
locations. However, you might need to change the value of the PSModulePath
environment variable.
For example, to temporarily add the C:\Program Files\Fabrikam\Modules
directory to $Env:PSModulePath for the current session, type:
$Env:PSModulePath = $Env:PSModulePath+";C:\Program Files\Fabrikam\Modules"
To change the value of PSModulePath in every session, edit the registry key
storing the PSModulePath values. The PSModulePath values are stored in the
registry as unexpanded strings. To avoid permanently saving the
PSModulePath values as expanded strings, use the GetValue() method on the
subkey and edit the value directly.
The following example adds the C:\Program Files\Fabrikam\Modules path to the
value of the PSModulePath environment variable without expanding the
unexpanded strings.
$key = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment')
$path = $key.GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
$path += ';%ProgramFiles%\Fabrikam\Modules'
$key.SetValue('PSModulePath',$path,[Microsoft.Win32.RegistryValueKind]::ExpandString)
To add a path to the user setting, use the following code:
$key = (Get-Item 'HKCU:\Environment')
$path = $key.GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
$path += ';%ProgramFiles%\Fabrikam\Modules'
$key.SetValue('PSModulePath',$path,[Microsoft.Win32.RegistryValueKind]::ExpandString)