How to change PowerShell default module installation folder?

耗尽温柔 提交于 2021-02-19 06:01:27

问题


Is there a way to change PowerShell module installation folder (the folder that modules are placed after Install-Module)? This is why I want to do this:

  • I'm on Windows 10, PowerShell 5.1.17763.503
  • My default installation folder is Documents\WindowsPowerShell\Modules
  • My Documents folder have been moved to a location containing , symbol (corporate policies)
  • PS has a bug loading .ps1 that contain classes and have , in the file path (similar to this issue.)

What I've tried:

  • I thought the installation folder is the first folder in the $env:PSModulePath and I can change it. When I've opened "Edit System Environment Variables" I saw the installation folder is not in the $env:PSModulePath. It's automatically added on the variable when you start PowerShell.

回答1:


There is no way to change the behaviour of Install-Module so it installs modules in a custom path.

However, You can use Install-Module [...] -Scope AllUsers to install the modules for all users. This would install the modules in $env:ProgramFiles\PowerShell\Modules, but this operation requires elevated permissions (a.k.a. Local Administrator rights).

If you download and install modules to a custom path yourself (or use an alternative implementation to Install-Module), you can modify $env:PSModulePath as you wish.

You can use a profile to patch the $env:PSModulePath every time you start a PowerShell session by adding this to one of your profiles:

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

From 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")
    


来源:https://stackoverflow.com/questions/56786702/how-to-change-powershell-default-module-installation-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!