Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
This article describes the purpose and usage of the $Env:PSModulePath
environment variable.
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:
$HOME\Documents\PowerShell\Modules. The specific location of the
Documents folder varies by version of Windows and when you use folder
redirection. Also, Microsoft OneDrive can change the location of your
Documents folder. To verify the location of your Documents folder, run
use the following command: [Environment]::GetFolderPath('MyDocuments').$HOME/.local/share/powershell/Modules folder.$Env:ProgramFiles\PowerShell\Modules./usr/local/share/powershell/Modules.$PSHOME\Modules.Note
Applications that include PowerShell modules can install modules in other
directories on Windows, such as the Program Files folder. The installer
package might not append the location to the $Env:PSModulePath.
The default locations for Windows PowerShell 5.1 are different from PowerShell 7.
$HOME\Documents\WindowsPowerShell\Modules.$Env:ProgramFiles\WindowsPowerShell\Modules.$PSHOME\Modules, which
is $Env:SystemRoot\System32\WindowsPowerShell\1.0\Modules.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 uses the following logic to construct the PSModulePath at
startup:
PSModulePath doesn't exist, combine CurrentUser, AllUsers, and
the $PSHOME modules pathsPSModulePath does exist:
PSModulePath contains $PSHOME modules path:
$PSHOME modules pathPSModulePath as defined since the user deliberately removed
the $PSHOME locationThe 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.
In Windows, for most environment variables, if the User-scoped variable exists, a new process uses that value only, even when a Machine-scoped variable of the same name exists. The path environment variables are treated differently.
On Windows, PSModulePath is treated similar to how the Path
environment variable is treated. Path is treated differently from other
environment variables. When a process is started, Windows combines the
User-scoped Path with the Machine-scoped Path.
PSModulePathPSModulePath environment variable
PSModulePath to the end following the semantics
of the PATH environment variableSystem32 path comes from the machine defined PSModulePath
so doesn't need to be added explicitlyPSModulePath$PSHOME paths in that order
powershell.config.json contains a user scoped PSModulePath, use that
instead of the default for the userpowershell.config.json contains a system scoped PSModulePath, use
that instead of the default for the systemNon-Windows systems don't have a separation of User and System environment
variables. PSModulePath is inherited and the PS7-specific paths are prefixed
if not already defined.
For this discussion, Windows PowerShell means both powershell.exe and
powershell_ise.exe.
The value of $Env:PSModulePath is copied to WinPSModulePath with the
following modifications:
$PSHOME module pathThe PS7 paths are removed so that PS7 modules don't get loaded in Windows
PowerShell. The WinPSModulePath value is used when starting Windows
PowerShell.
The PowerShell 7 startup continues as-is with the addition of inheriting paths that Windows PowerShell added. Since the PS7-specific paths are prefixed, there's no functional issue.
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.
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"
The semi-colon (;) in the command separates the new path from the path that
precedes it in the list. On non-Windows platforms, the colon (:) separates
the path locations in the environment variable.
To change the value of PSModulePath for every session in a non-Windows
environment, add the previous command to your PowerShell profile.
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)
PowerShell feedback
PowerShell is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in