Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change PowerShell default module installation folder?

Tags:

powershell

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.
like image 528
f0rt Avatar asked Jun 27 '19 08:06

f0rt


People also ask

How do I change the directory of a PowerShell module?

To add or change files in the $env:Windir\System32 directory, start PowerShell with the Run as administrator option. You can change the default module locations on your system by changing the value of the PSModulePath environment variable, $Env:PSModulePath .

Where is PS module path?

On Windows, the location of the user-specific CurrentUser scope is the $HOME\Documents\PowerShell\Modules folder. The location of the AllUsers scope is $env:ProgramFiles\PowerShell\Modules . On non-Windows systems, the location of the user-specific CurrentUser scope is the $HOME/. local/share/powershell/Modules folder.


1 Answers

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")
    
like image 200
mehmetseckin Avatar answered Nov 15 '22 09:11

mehmetseckin