关于 PSModulePathAbout PSModulePath
$env:PSModulePath
环境变量包含一个文件夹位置列表,将在其中进行搜索以查找模块和资源。The $env:PSModulePath
environment variable contains a list of folder locations that are searched to find modules and resources. PowerShell 会以递归方式在每个文件夹中搜索模块 (.psd1
或 .psm1
) 文件。PowerShell recursively searches each folder for module (.psd1
or .psm1
) files.
默认情况下,分配到的有效位置 $env:PSModulePath
为:By default, the effective locations assigned to $env:PSModulePath
are:
系统范围的位置:这些文件夹包含 PowerShell 附带的模块。System-wide locations: These folders contain modules that ship with PowerShell. 这些模块存储在文件夹中
$PSHOME\Modules
。These modules are store in the$PSHOME\Modules
folder. 这也是安装 Windows 管理模块的位置。This is also the location where the Windows management modules are installed.用户安装的模块:这些是用户安装的模块。User-installed modules: These are modules installed by the user.
Install-Module
具有 作用域 参数,该参数可用于指定是为当前用户还是为所有用户安装该模块。Install-Module
has a Scope parameter that allows you to specify whether the module is installed for the current user or for all users. 有关详细信息,请参阅 Install-Module。For more information, see Install-Module.- 在 Windows 上,特定于用户的 CurrentUser 范围是
$HOME\Documents\PowerShell\Modules
文件夹。On Windows, the location of the user-specific CurrentUser scope is the$HOME\Documents\PowerShell\Modules
folder. AllUsers 作用域的位置为$env:ProgramFiles\PowerShell\Modules
。The location of the AllUsers scope is$env:ProgramFiles\PowerShell\Modules
. - 在非 Windows 系统上,特定于用户的 CurrentUser 范围是
$HOME/.local/share/powershell/Modules
文件夹。On non-Windows systems, the location of the user-specific CurrentUser scope is the$HOME/.local/share/powershell/Modules
folder. AllUsers 作用域的位置为/usr/local/share/powershell/Modules
。The location of the AllUsers scope is/usr/local/share/powershell/Modules
.
- 在 Windows 上,特定于用户的 CurrentUser 范围是
此外,在其他目录中安装模块的安装程序(如 Program Files 目录)可以将它们的位置附加到的值 $env:PSModulePath
。In addition, setup programs that install modules in other directories, such as the Program Files directory, can append their locations to the value of $env:PSModulePath
.
备注
在 Windows 上,用户特定的位置是 PowerShell\Modules
位于用户配置文件中的 Documents 文件夹中的文件夹。On Windows, the user-specific location is the PowerShell\Modules
folder located in the Documents folder in your user profile. 该位置的特定路径因 Windows 版本而异,无论是否正在使用文件夹重定向。The specific path of that location varies by version of Windows and whether or not you are using folder redirection. Microsoft OneDrive 还可以更改 文档 文件夹的位置。Microsoft OneDrive can also change the location of your Documents folder. 可以使用以下命令来验证 Documents 文件夹的位置: [Environment]::GetFolderPath('MyDocuments')
。You can verify the location of your Documents folder using the following command: [Environment]::GetFolderPath('MyDocuments')
.
修改 PSModulePathModifying PSModulePath
若要更改当前会话的默认模块目录,请使用以下命令格式来更改 PSModulePath
环境变量的值。To change the default module directories for the current session, use the following command format to change the value of the PSModulePath
environment variable.
例如,若要将 C:\Program Files\Fabrikam\Modules
目录添加到 PSModulePath 环境变量的值,请键入:For example, to add the C:\Program Files\Fabrikam\Modules
directory to the value of the PSModulePath environment variable, 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. 在非 Windows 平台上,冒号 (:
) 将环境变量中的路径位置隔开。On non-Windows platforms, the colon (:
) separates the path locations in the environment variable.
若要 PSModulePath
在每个会话中更改的值,请将上一个命令添加到 PowerShell 配置文件,或使用 环境 类的 SetEnvironmentVariable 方法。To change the value of PSModulePath
in every session, add the previous command to your PowerShell profile or use the SetEnvironmentVariable method of the Environment class.
下面的命令使用 GetEnvironmentVariable 方法来获取的计算机设置 PSModulePath
,并使用 SetEnvironmentVariable 方法将 C:\Program Files\Fabrikam\Modules
路径添加到值中。The following command uses the GetEnvironmentVariable method to get the machine setting of PSModulePath
and the SetEnvironmentVariable method to add the C:\Program Files\Fabrikam\Modules
path to the value.
$path = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
$newpath = $path + ';C:\Program Files\Fabrikam\Modules'
[Environment]::SetEnvironmentVariable('PSModulePath', $newpath, 'Machine')
若要将路径添加到用户设置,请将 "目标" 值更改为 " 用户 "。To add a path to the user setting, change the target value to User.
$path = [Environment]::GetEnvironmentVariable('PSModulePath', 'User')
$newpath = $path + ';C:\Program Files\Fabrikam\Modules'
[Environment]::SetEnvironmentVariable('PSModulePath', $newpath, 'User')
有关 system.web 类的方法的详细信息,请参阅 环境方法。For more information about the methods of the System.Environment class, see Environment Methods.
PowerShell PSModulePath 构造PowerShell PSModulePath construction
$env:PSModulePath
每次启动 PowerShell 时,都会构造的值。The value of $env:PSModulePath
is constructed each time PowerShell starts.
该值因 PowerShell 版本和启动方式而异。The value varies by version of PowerShell and how it is launched.
Windows PowerShell 启动Windows PowerShell startup
Windows PowerShell 在启动时使用以下逻辑来构造 PSModulePath
:Windows PowerShell uses the following logic to construct the PSModulePath
at startup:
- 如果
PSModulePath
不存在,则将 CurrentUser 、 AllUsers 和$PSHOME
模块路径组合在一起IfPSModulePath
doesn't exist, combine CurrentUser , AllUsers , and the$PSHOME
modules paths - 如果
PSModulePath
存在,则:IfPSModulePath
does exist:- 如果
PSModulePath
包含$PSHOME
模块路径:IfPSModulePath
contains$PSHOME
modules path:- 在模块路径之前插入 AllUsers 模块路径
$PSHOME
AllUsers modules path is inserted before$PSHOME
modules path
- 在模块路径之前插入 AllUsers 模块路径
- else:else:
- 只需使用
PSModulePath
自用户特意删除位置后定义$PSHOME
的Just usePSModulePath
as defined since the user deliberately removed the$PSHOME
location
- 只需使用
- 如果
仅当用户范围不存在时, CurrentUser 模块路径才带有前缀 $env:PSModulePath
。The CurrentUser module path is prefixed only if User scope $env:PSModulePath
doesn't exist. 否则,将 $env:PSModulePath
按定义使用用户作用域。Otherwise, the User scope $env:PSModulePath
is used as defined.
PowerShell Core 6 启动PowerShell Core 6 startup
如果 PowerShell Core 6 $env:PSModulePath
检测到它是从 PowerShell 启动的,则不使用的内容。PowerShell Core 6 doesn't use contents of $env:PSModulePath
if it detects it was started from PowerShell. 它将覆盖它:It overwrites it with:
- CurrentUser 模块路径 + AllUsers 模块路径 +
$PSHOME
模块路径 + Windows PowerShell$PSHOME
模块路径。CurrentUser modules path + AllUsers modules path +$PSHOME
modules path + Windows PowerShell$PSHOME
modules path.
PowerShell 7 启动PowerShell 7 startup
在 Windows 中,对于大多数环境变量,如果存在用户范围的变量,新进程将仅使用该值,即使存在同名的计算机范围的变量。In Windows, for most environment variables, if the User-scoped variable exists, a new process uses that value only even if a Machine-scoped variable of the same name exists.
在 PowerShell 7 中, PSModulePath
的处理方式类似于 Path
Windows 上环境变量的处理方式。In PowerShell 7, PSModulePath
is treated similar to how the Path
environment variable is treated on Windows. 在 Windows 上,的 Path
处理方式与其他环境变量不同。On Windows, Path
is treated differently from other environment variables. 当进程启动时,Windows 会将用户范围 Path
与计算机范围合并 Path
。When a process is started, Windows combines the User-scoped Path
with the Machine-scoped Path
.
- 检索用户范围
PSModulePath
Retrieve the User-scopedPSModulePath
- 与处理继承的
PSModulePath
环境变量比较Compare to process inheritedPSModulePath
environment variable- 如果相同:If the same:
- 在 AllUsers
PSModulePath
Path
环境变量的语义后面追加 AllUsers 到末尾Append the AllUsersPSModulePath
to the end following the semantics of thePath
environment variable - Windows
System32
路径来自定义的计算机PSModulePath
,因此无需显式添加The WindowsSystem32
path comes from the machine definedPSModulePath
so does not need to be added explicitly
- 在 AllUsers
- 如果不同,则视为用户显式修改了它,并且不追加 AllUsers
PSModulePath
If different, treat as though user explicitly modified it and don't append AllUsersPSModulePath
- 如果相同:If the same:
- 按顺序为 PS7 用户、系统和
$PSHOME
路径提供前缀Prefix with PS7 User, System, and$PSHOME
paths in that order- 如果
powershell.config.json
包含用户范围的PSModulePath
,请使用它,而不是用户的默认值Ifpowershell.config.json
contains a user scopedPSModulePath
, use that instead of the default for the user - 如果
powershell.config.json
包含系统范围PSModulePath
,请使用此值,而不使用系统的默认值Ifpowershell.config.json
contains a system scopedPSModulePath
, use that instead of the default for the system
- 如果
Unix 系统没有隔离用户和系统环境变量。Unix systems don't have a separation of User and System environment variables.
PSModulePath
是继承的,如果尚未定义 PS7 特定路径,则会将其作为前缀。PSModulePath
is inherited and the PS7-specific paths are prefixed if not already defined.
从 PowerShell 7 启动 Windows PowerShellStarting Windows PowerShell from PowerShell 7
对于此讨论, Windows PowerShell 既表示 powershell.exe
又是 powershell_ise.exe
。For this discussion, Windows PowerShell means both powershell.exe
and powershell_ise.exe
.
将的值 $env:PSModulePath
复制到, WinPSModulePath
并进行以下修改:The value of $env:PSModulePath
is copied to WinPSModulePath
with the following modifications:
- 删除用户模块路径 PS7Remove PS7 the User module path
- 删除系统模块路径 PS7Remove PS7 the System module path
- 删除模块路径中的 PS7
$PSHOME
Remove PS7 the$PSHOME
module path
删除 PS7 路径,以便不会在 Windows PowerShell 中加载 PS7 模块。The PS7 paths are removed so that PS7 modules don't get loaded in Windows PowerShell. WinPSModulePath
启动 Windows PowerShell 时使用该值。The WinPSModulePath
value is used when starting Windows PowerShell.
从 Windows PowerShell 启动 PowerShell 7Starting PowerShell 7 from Windows PowerShell
PowerShell 7 启动将按原样继续,并添加 Windows PowerShell 添加的继承路径。The PowerShell 7 startup continues as-is with the addition of inheriting paths that Windows PowerShell added. 由于 PS7 特定的路径带有前缀,因此不存在功能问题。Since the PS7-specific paths are prefixed, there is no functional issue.
从 PowerShell 7 启动 PowerShell 6Starting PowerShell 6 from PowerShell 7
PowerShell Core 6 将覆盖 $env:PSModulePath
。PowerShell Core 6 overwrites $env:PSModulePath
. 未进行任何更改。No changes are made.
从 PowerShell 6 启动 PowerShell 7Starting PowerShell 7 from PowerShell 6
PowerShell 7 启动将按原样继续,并添加 PowerShell Core 6 添加的继承路径。The PowerShell 7 startup continues as-is with the addition of inheriting paths that PowerShell Core 6 added. 由于 PS7 特定的路径带有前缀,因此不存在功能问题。Since the PS7-specific paths are prefixed, there is no functional issue.
模块搜索行为Module search behavior
PowerShell 会以递归方式搜索 PSModulePath 中的每个文件夹 (.psd1
或 .psm1
) 文件。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
默认情况下,当找到多个版本时,PowerShell 将加载模块的最高版本号。By default, PowerShell loads the highest version number of a module when multiple versions are found. 若要加载特定版本,请使用 Import-Module
With FullyQualifiedName 参数。To load a specific version, use Import-Module
with the FullyQualifiedName parameter. 有关详细信息,请参阅 import-module。For more information, see Import-Module.