powershell - PSModulePath 环境属性是如何组成的?

标签 powershell

这个问题与我的另一篇文章有​​关 - Using both desktop Powershell 5.1 and Powershell Core 6.1

基本上,问题的根源在于,当我打开桌面 Powershell 并检查 $env:PSModulePath 时,我在那里看到了 PS.Core 模块路径。

请注意:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\me> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.503
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.503
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\Users\me> $env:PSModulePath -split ';'
C:\Users\me\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\6\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
PS C:\Users\me>

注意 c:\program files\powershell\6\Modules

现在,当我在控制面板中检查环境变量时,我看到了不同的画面:

用户

enter image description here

系统

enter image description here

我什至没有$PROFILE脚本:

PS C:\Users\me> Test-Path $PROFILE
False
PS C:\Users\me>

可以看到控制面板中没有提到 PS.Core 6.1 模块路径,但在实际的 PSModulePath 环境变量中找到了它。

那么,我的问题是 - PSModulePath 是如何真正组成的?

我在 Microsoft 文档中找不到它(我的意思是我确实发现它是一个环境变量,但正如人们所见,它还有更多内容)。我一定遗漏了一些明显的东西。

编辑 1

我打开 Process Explorer 并检查了父进程的环境 - explorer.exe。它的 PSModulePath 是:

C:\Users\mkharitonov\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\6\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

但是杀掉explorer.exe再运行,发现PS.Core模块路径消失了。因此重新打开 Powershell 会显示正确的模块路径。

我想当我安装 PS.Core 6.1 时它以某种方式修改了 explorer.exe 的环境。还是 VS Code?无论如何,重新启动资源管理器可以解决问题。

我将不得不深入研究它并向 PS.Core 或 VS Code 打开一个错误。

最佳答案

当 PowerShell 启动时,它会尝试 set the PSModulePath env var通过:

  1. 从主机进程读取 %PSModulePath% 的现有值
  2. 从主机应用程序的配置中读取当前用户和所有用户的默认模块路径
  3. 如果在 Windows 上,还要读取 %windir%\system32 模块文件夹
  4. 通过组合所有三个来更新 $env:PSModulePath 的值

如果您在那里看到一个意外的值,pwsh.exe 可能只是从父进程继承了它,并且包含在步骤 1 中

关于powershell - PSModulePath 环境属性是如何组成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56888806/

相关文章:

powershell - 如何在 Powershell 中迭代键/值输出?

powershell - Azure PowerShell Start-AzureAutomationRunbook 未指定默认订阅

powershell - 尝试在包含大量文件的目录中获取唯一的扩展名列表非常缓慢

powershell - 脚本中的代码有所不同,具体取决于从另一个脚本或shell调用

powershell - HTMLagilityPack 结合 Powershell、Windows 身份验证

json - 不知道如何在Powershell中获取JSON对象的名称字段?

Powershell Select-Object -match 还是?

windows - 为什么 'Measure-Object -InputObject $foo' 与 PowerShell 中的 '$foo | Measure-Object' 不同?

arrays - 将数组作为单独的参数传递给 PowerShell 脚本

PowerShell 范围标识符