14

Currently we are installing some modules using the command below, but it installs modules in C:\Program Files\WindowsPowerShell\Modules.

Install-Module -Name XXX -RequiredVersion XXX -Repository XXX -Scope AllUsers

Our requirement is to install this module in the E:\Modules folder. For that I updated the PSModulePath environment variable as below. (https://msdn.microsoft.com/en-us/library/dd878350(v=vs.85).aspx)

$p = [Environment]::GetEnvironmentVariable("PSModulePath")
$p += ";E:\Modules"
[Environment]::SetEnvironmentVariable("PSModulePath",$p)

But it still installs in C:\Program Files\WindowsPowerShell\Modules.

How do I update PSModulePath to E:\Modules before installing modules?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
mit
  • 1,763
  • 4
  • 16
  • 27

3 Answers3

19

You can download the module zip manually using the Save-Module command.

Find-Module -Name 'XXX' -Repository 'PSGallery' | Save-Module -Path 'E:\Modules'

From here you can either import the module using a fully qualified name like so:

Import-Module -FullyQualifiedName 'E:\Modules\XXX'

Or by adding your destination folder to the PSModulePath like you did before.

$modulePath = [Environment]::GetEnvironmentVariable('PSModulePath')
$modulePath += ';E:\Modules'
[Environment]::SetEnvironmentVariable('PSModulePath', $modulePath)

You can then check if the module has been imported using the Get-Module cmdlet.


If you are using the Import-Module command it can get a bit painful, especially if you have lots of modules. So you could wrap the approach in a function like this:

function Install-ModuleToDirectory {
    [CmdletBinding()]
    [OutputType('System.Management.Automation.PSModuleInfo')]
    param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $Name,

        [Parameter(Mandatory = $true)]
        [ValidateScript({ Test-Path $_ })]
        [ValidateNotNullOrEmpty()]
        $Destination
    )

    # Is the module already installed?
    if (-not (Test-Path (Join-Path $Destination $Name))) {
        # Install the module to the custom destination.
        Find-Module -Name $Name -Repository 'PSGallery' | Save-Module -Path $Destination
    }

    # Import the module from the custom directory.
    Import-Module -FullyQualifiedName (Join-Path $Destination $Name)

    return (Get-Module)
}

Install-ModuleToDirectory -Name 'XXX' -Destination 'E:\Modules'
Harry Sanderson
  • 343
  • 2
  • 15
1

In order to gain control over the module install path you need to stop using -Scope flag. When you don't specify a scope the default install location is the first path returned from the $env:PSModulePath environment variable. If you modify this variable directly in the script it will only persist for your session. This might be ideal for what you are doing.

First, add your custom path as the first item in the variable:

$env:PSModulePath = "E:\Modules;" + $env:PSModulePath

Then when you run your install it will use that path:

Install-Module -Name XXX -RequiredVersion XXX -Repository XXX

You can then optionally make that setting permanent:

[Environment]::SetEnvironmentVariable("PSModulePath", $env:PSModulePath)

As described in the Microsoft Doc.

HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • 1
    I set my custom path to the first entry in PSModulePath using the command above. Install-Module still installed under the default C:/Program Files/... path. So it not only skipped my custom path but also my default user module path. – Blaisem Jul 12 '21 at 09:50
  • Take a look at the contents of `$env:PSModulePath` by using `Write-Output`. What is the first path listed? Is it the place the install went to? – HackSlash Jul 15 '21 at 15:22
  • 1
    The first path listed is my custom path. The path it installed to is C:\Program Files\WindowsPowerShell\Modules. Have you tried it yourself to see if it works? Maybe it's the PS version? Mine is PowerShell 5.1.14393. – Blaisem Jul 19 '21 at 12:02
  • I have tried it and this aspect of PowerShell does behave like it is described in the documentation. If it didn't, that would be a bug. – HackSlash Jul 19 '21 at 16:49
  • ftr - doesn't work for me either. Adding a local, writable folder as the first folder in my PSModulePath and using Install-Module without scope parameter, Install-Module tries to install in the allusers location which requires administrator rights. – Lieven Keersmaekers Oct 18 '21 at 05:47
  • I'm not sure how this ever worked, unless Microsoft changed the docs retroactively. They're pretty clear that PSGet only installs to $env:ProgramFiles\PowerShell\Modules or $HOME\Documents\PowerShell\Modules https://learn.microsoft.com/en-us/powershell/module/powershellget/install-module – pl4nty Jun 02 '23 at 00:45
-1

$env:PSModulePath is an environment variable which is used to search for modules when you do Import-Module and also to do module auto load from PS V3 onwards.

if you go I through the help file for Install-Module , I can't see an option to provide install path for module.

So as a workaround, you could have a copy job based on the module name(same will be folder name for every module) to your custom path.

Regards,

Kvprasoon

Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26