修改 PSModulePath 安裝路徑Modifying the PSModulePath Installation Path

PSModulePath環境變數會儲存安裝在磁片上之模組位置的路徑。The PSModulePath environment variable stores the paths to the locations of the modules that are installed on disk. 當使用者未指定模組的完整路徑時,PowerShell 會使用此變數來尋找模組。PowerShell uses this variable to locate modules when the user does not specify the full path to a module. 此變數中的路徑會依照其出現的順序進行搜尋。The paths in this variable are searched in the order in which they appear.

當 PowerShell 啟動時, PSModulePath 會建立為具有下列預設值的系統內容變數: $HOME\Documents\PowerShell\Modules; $PSHOME\Modules 在 Windows 和 $HOME/.local/share/powershell/Modules: usr/local/share/powershell/Modules Linux 或 Mac 上,以及 $HOME\Documents\WindowsPowerShell\Modules; $PSHOME\Modules windows PowerShell。When PowerShell starts, PSModulePath is created as a system environment variable with the following default value: $HOME\Documents\PowerShell\Modules; $PSHOME\Modules on Windows and $HOME/.local/share/powershell/Modules: usr/local/share/powershell/Modules on Linux or Mac, and $HOME\Documents\WindowsPowerShell\Modules; $PSHOME\Modules for Windows PowerShell.

注意

使用者專屬的CurrentUser位置是 WindowsPowerShell\Modules 位於使用者設定檔中 [] 位置的資料夾。The user-specific CurrentUser location is the WindowsPowerShell\Modules folder located in the Documents location in your user profile. 該位置的特定路徑會因 Windows 版本,以及您是否使用資料夾重新導向而有所不同。The specific path of that location varies by version of Windows and whether or not you are using folder redirection. 根據預設,在 Windows 10 上,該位置是 $HOME\Documents\WindowsPowerShell\ModulesBy default, on Windows 10, that location is $HOME\Documents\WindowsPowerShell\Modules.

若要查看 PSModulePath 變數To view the PSModulePath variable

若要查看變數中指定的路徑 PSModulePath ,請輸入下列命令:To view the paths that are specified in the PSModulePath variable, type the following command:

$env:PSModulePath

若要將位置新增至 PSModulePath 變數To add locations to the PSModulePath variable

若要將路徑新增至此變數,請使用下列其中一種方法: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 + "$([System.IO.Path]::PathSeparator)$MyModulePath"

  • 若要新增每次開啟會話時可用的持續性值,請將上述命令新增至 PowerShell 設定檔檔案( $PROFILE ) >To add a persistent value that is available whenever a session is opened, add the above command to a PowerShell profile file ($PROFILE)>

    如需設定檔的詳細資訊,請參閱 about_ProfilesFor more information about profiles, see about_Profiles.

  • 若要將持續變數新增至登錄,請 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.

  • 若要使用腳本來加入持續性變數,請使用SetEnvironmentVariablesystem.web類別上的 .net 方法。To add a persistent variable by using a script, use the .Net method SetEnvironmentVariable on the System.Environment class. 例如,下列腳本會將電腦的 C:\Program Files\Fabrikam\Module 環境變數值的路徑新增至 PSModulePathFor example, the following script adds the C:\Program Files\Fabrikam\Module path to the value of the PSModulePath environment variable for the computer. 若要將路徑新增至使用者 PSModulePath 環境變數,請將目標設為 "user"。To add the path to the user PSModulePath environment variable, set the target to "User".

    $CurrentValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
    [Environment]::SetEnvironmentVariable("PSModulePath", $CurrentValue + [System.IO.Path]::PathSeparator + "C:\Program Files\Fabrikam\Modules", "Machine")
    
    

若要從 PSModulePath 中移除位置To remove locations from the PSModulePath

您可以使用類似的方法來移除變數中的路徑:例如, $env:PSModulePath = $env:PSModulePath -replace "$([System.IO.Path]::PathSeparator)c:\\ModulePath" 會從目前的會話移除c:\ModulePath路徑。You can remove paths from the variable using similar methods: for example, $env:PSModulePath = $env:PSModulePath -replace "$([System.IO.Path]::PathSeparator)c:\\ModulePath" will remove the c:\ModulePath path from the current session.

另請參閱See Also

撰寫 Windows PowerShell 模組Writing a Windows PowerShell Module

about_Modulesabout_Modules