How to install module into Custom directory? How to install module into Custom directory? powershell powershell

How to install module into Custom directory?


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'


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.


$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