You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux(CentOS7.9)下持久化配置PowerShell7的PSModulePath变量

解决CentOS 7.9上PowerShell 7的PSModulePath持久化问题

临时修改$env:PSModulePath只会在当前PowerShell会话生效,重启后会自动重置,要实现持久化配置可以用以下两种方式:

方法一:用户级配置(仅当前用户生效)

  1. 先检查PowerShell的用户配置文件是否存在,执行:
    Test-Path $PROFILE
    
  2. 如果返回False,创建配置文件:
    New-Item -Path $PROFILE -ItemType File -Force
    
  3. 编辑这个配置文件(比如用nano $PROFILE),添加一行:
    $env:PSModulePath += ":/opt/MyModules"
    
  4. 保存后重启PowerShell,执行$env:PSModulePath验证自定义路径是否已添加。

方法二:系统级配置(所有用户生效)

方式A:修改PowerShell系统配置文件

  1. 进入PowerShell系统配置目录:
    cd /etc/powershell
    
  2. 编辑或创建profile.ps1文件:
    nano profile.ps1
    
  3. 添加同样的配置行:
    $env:PSModulePath += ":/opt/MyModules"
    
  4. 保存后,所有用户重启PowerShell都会加载这个配置。

方式B:修改系统全局环境变量

  1. 编辑系统全局环境变量文件,比如/etc/profile:
    nano /etc/profile
    
  2. 添加一行:
    export PSModulePath="$PSModulePath:/opt/MyModules"
    
  3. 执行source /etc/profile让当前会话立即生效,重启系统或PowerShell后配置会持久化。

内容的提问来源于stack exchange,提问作者LightningWar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 12:21:02