powershell - 如何更改 PowerShell 默认模块安装文件夹?

标签 powershell

有没有办法更改 PowerShell 模块安装文件夹(模块在 Install-Module 之后放置的文件夹)?
这就是为什么我想这样做:

  • 我在 Windows 10 上,PowerShell 5.1.17763.503
  • 我的默认安装文件夹是Documents\WindowsPowerShell\Modules
  • 我的文档文件夹已移至包含 , 的位置符号(公司政策)
  • PS 有一个错误加载 .ps1 包含类并具有 ,在文件路径中(类似于 this issue 。)

  • 我试过的:
  • 我认为安装文件夹是 $env:PSModulePath 中的第一个文件夹,我可以更改它。当我打开“编辑系统环境变量”时,我看到安装文件夹不在 $env:PSModulePath 中。 .当您启动 PowerShell 时,它会自动添加到变量中。
  • 最佳答案

    无法更改 Install-Module 的行为因此它将模块安装在自定义路径中。

    但是,您可以使用 Install-Module [...] -Scope AllUsers为所有用户安装模块。这将在 $env:ProgramFiles\PowerShell\Modules 中安装模块, 但此操作需要 提升权限 (又名本地管理员权限)。

    如果您自己下载并安装模块到自定义路径(或使用 Install-Module 的替代实现),您可以修改 $env:PSModulePath如你所愿。

    您可以使用配置文件来修补 $env:PSModulePath每次通过将其添加到您的配置文件之一来启动 PowerShell session 时:

    # Prepend custom module path.
    $env:PSModulePath = ((@("C:\mymodulepath") + ($env:PSModulePath -split ";")) -join ";")
    

    来自 Modifying the PSModulePath Installation Path

    To add paths to this variable, use one of the following methods:

    • To add a temporary value that is available only for the current session, run the following command at the command line:

      $env:PSModulePath = $env:PSModulePath + ";c:\ModulePath"
      
    • To add a persistent value that is available whenever a session is opened, add the following command to a Windows PowerShell profile:

      $env:PSModulePath = $env:PSModulePath + ";c:\ModulePath"
      

    For more information about profiles, see about_Profiles in the Microsoft TechNet library.

    • To add a persistent variable to the registry, create a new user environment variable called PSModulePath using the Environment Variables Editor in the System Properties dialog box.

    • To add a persistent variable by using a script, use the SetEnvironmentVariable method on the Environment class. For example, the following script adds the "C:\Program Files\Fabrikam\Module" path to the value of the PSModulePath environment variable for the computer. To add the path to the user PSModulePath environment variable, set the target to "User".

      $CurrentValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
      [Environment]::SetEnvironmentVariable("PSModulePath", $CurrentValue + ";C:\Program Files\Fabrikam\Modules", "Machine")
      

    关于powershell - 如何更改 PowerShell 默认模块安装文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56786702/

    相关文章:

    powershell - 比较2个文件夹并将路径保存到变量中?

    powershell - PowerShell Get-FileHash的括号和圆括号之间有什么区别?

    Powershell:从多个json对象中提取值

    powershell - 带和不带引号的Powershell奇数返回DFSR卷路径

    powershell - 使用PowerShell编辑文件的元数据(Windows文件资源管理器中文件的“详细信息”选项卡)

    powershell - 等待用户输入超时

    c# - 创建 RunSpace 时如何使用 PowerShell 2 或 3?

    git - 如何使用 PowerShell 删除 Git 中所有 merge 的本地分支

    PowerShell:是否可以编写一个引用其文件夹中的文件的脚本,无论从哪里调用它仍然可以工作?

    PowerShell 使用字节数组读取和写入压缩文件