简体   繁体   English

更改 Powershell 中的 PSModulePath 5

[英]Changing the PSModulePath in Powershell 5

I've used the following to permanently change the PSModulePath:我已使用以下内容永久更改 PSModulePath:

[Environment]::SetEnvironmentVariable('PSModulePath', "ABC", "Machine")

This works fine when I call the below (it returns "ABC"):当我调用以下代码时,这很好用(它返回“ABC”):

[Environment]::GetEnvironmentVariable('PSModulePath', "Machine")

But in any Powershell Session when I run:但是在我运行的任何 Powershell Session 中:

$env:PSModulePath

I get:我得到:

C:\Users\myname\Documents\WindowsPowerShell\Modules;ABC

Where is this path coming from, is it PS5 magic?这条路从何而来,是PS5的魔法吗? I've checked the "User" target and this is blank.我检查了“用户”目标,这是空白的。 It's as if something is pre-pending the PSModulePath with this default path?就好像有什么东西在 PSModulePath 这个默认路径之前?

The environment drive Env: contains the environment variables specific to the current user's session ( source ).环境驱动Env:包含特定于当前用户的session( source )的环境变量。 It is equivalent to the Process scope.它相当于Process scope。 So所以

[Environment]::GetEnvironmentVariable('PSModulePath', 'Process')

should be equivalent to应该相当于

$env:PSModulePath

The Process scope contains the environment variables of a particular process. Process scope 包含特定进程的环境变量。 It gets constructed like this ( source ):它是这样构造的( 来源):

This list of variables is inherited from the parent process and is constructed from the variables in the Machine and User scopes.此变量列表继承自父进程,并由MachineUser范围内的变量构成。

As you checked both, the Machine and the User scope, and did not find the path, it has to come from the parent process, which is PowerShell itself.当您检查MachineUser scope 时,没有找到路径,它必须来自父进程,即 PowerShell 本身。 And this is indeed the case as can be read here :确实如此,可以在这里阅读:

The CurrentUser module path is prefixed only if User scope $env:PSModulePath doesn't exist.仅当用户 scope $env:PSModulePath不存在时, CurrentUser模块路径才会作为前缀。 Otherwise, the User scope $env:PSModulePath is used as defined.否则,按照定义使用用户 scope $env:PSModulePath

As you confirmed in your question,正如您在问题中确认的那样,

[Environment]::GetEnvironmentVariable('PSModulePath', 'User')

is empty and thus $env:PSModulePath has been prefixed by the CurrentUser module path, which is $HOME\Documents\PowerShell\Modules or $HOME\Documents\WindowsPowerShell\Modules depending on your Windows version .为空,因此$env:PSModulePathCurrentUser模块路径为前缀,即$HOME\Documents\PowerShell\Modules$HOME\Documents\WindowsPowerShell\Modules ,具体取决于您的Windows 版本

You can read more about environment variables in my answer here .您可以在我的回答中阅读有关环境变量的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM