如何更改PowerShell默认模块安装文件夹? [英] How to change PowerShell default module installation folder?
问题描述
有没有办法更改PowerShell模块安装文件夹(模块放置在Install-Module之后的文件夹)?这就是我想这样做的原因:
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:
- 我使用的是 Windows 10,PowerShell 5.1.17763.503
- 我的默认安装文件夹是
Documents\WindowsPowerShell\Modules
- 我的文档文件夹已移至包含
,
符号(公司政策)的位置 - PS 有一个错误加载 .ps1 包含类并且在文件路径中有
,
(类似于 这个问题.)
- 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.)
我尝试过的:
- 我认为安装文件夹是 $env:PSModulePath 中的第一个文件夹,我可以更改它.当我打开编辑系统环境变量"时,我看到安装文件夹不在
$env:PSModulePath
中.当您启动 PowerShell 时,它会自动添加到变量中.
- 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.
推荐答案
没有办法改变 Install-Module
的行为,因此它在自定义路径中安装模块.
There is no way to change the behaviour of Install-Module
so it installs modules in a custom path.
但是,您可以使用 Install-Module [...] -Scope AllUsers
为所有用户安装模块.这将在 $env:ProgramFiles\PowerShell\Modules
中安装模块,但此操作需要提升权限(也称为本地管理员权限).
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).
如果您自己下载并安装模块到自定义路径(或使用 Install-Module
的替代实现),您可以根据需要修改 $env:PSModulePath
.
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.
您可以在每次启动 PowerShell 会话时使用配置文件修补 $env:PSModulePath
,方法是将其添加到您的配置文件之一:
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 ";")
要添加此变量的路径,请使用以下方法之一:
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"
要添加在会话打开时可用的持久值,请将以下命令添加到 Windows PowerShell 配置文件:
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"
有关配置文件的详细信息,请参阅about_Profiles 在Microsoft TechNet 库.
For more information about profiles, see about_Profiles in the Microsoft TechNet library.
要将持久变量添加到注册表,请使用 Environment 创建一个名为
PSModulePath
的新用户环境变量系统属性对话框中的变量编辑器.
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.
要使用脚本添加持久变量,请使用 Environment 类上的 SetEnvironmentVariable
方法.例如,以下脚本添加了 "C:\Program Files\Fabrikam\Module"
路径到 PSModulePath
环境变量的值电脑.将路径添加到用户 PSModulePath
环境变量,将目标设置为 "User"
.
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默认模块安装文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!