Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改Powershell 5中的PSModulePath_Powershell_Environment Variables_Powershell 5.0 - Fatal编程技术网

更改Powershell 5中的PSModulePath

更改Powershell 5中的PSModulePath,powershell,environment-variables,powershell-5.0,Powershell,Environment Variables,Powershell 5.0,我已使用以下命令永久更改PSModulePath: [Environment]::SetEnvironmentVariable('PSModulePath', "ABC", "Machine") 当我调用下面的函数时(它返回“ABC”): 但在我运行的任何Powershell会话中: $env:PSModulePath 我得到: C:\Users\myname\Documents\WindowsPowerShell\Modules;ABC 这条路从哪

我已使用以下命令永久更改PSModulePath:

[Environment]::SetEnvironmentVariable('PSModulePath', "ABC", "Machine")
当我调用下面的函数时(它返回“ABC”):

但在我运行的任何Powershell会话中:

$env:PSModulePath
我得到:

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

这条路从哪里来,是PS5魔法吗?我已经检查了“用户”目标,这是空的。就好像有什么东西用这个默认路径预先挂起PSModulePath?

环境驱动器
Env:
包含特定于当前用户会话()的环境变量。它相当于
过程
范围。所以

[Environment]::GetEnvironmentVariable('PSModulePath', 'Process')
应该相当于

$env:PSModulePath
流程
范围包含特定流程的环境变量。它的构造如下():

此变量列表继承自父进程,并由计算机和用户范围中的变量构成

当您同时检查
机器
用户
范围时,没有找到路径,它必须来自父进程,即PowerShell本身。这确实是可以理解的情况:

只有当用户范围
$env:PSModulePath
不存在时,CurrentUser模块路径才会加前缀。否则,用户范围
$env:PSModulePath
将按定义使用

正如你在问题中确认的那样

[Environment]::GetEnvironmentVariable('PSModulePath', 'User')
是空的,因此,
$env:PSModulePath
已由当前用户模块路径作为前缀,该路径是
$HOME\Documents\PowerShell\Modules
$HOME\Documents\WindowsPowerShell\Modules
,具体取决于您的用户

你可以在我的答案中阅读更多关于环境变量的内容

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