0

We have a few modules residing on an SMB file share. We want to extend the module path where PowerShell looks for scripts (temporarily, not permanently).

By default, the contents of $env:PSModulePath environment variable is this:

> $env:PSModulePath -split ';'

C:\Users\xxx\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\windows\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files\Microsoft Monitoring Agent\Agent\PowerShell\

As documented by Microsoft, I wanted to extend this environment variable by one or more locations, so that calling the Import-Module or even a #Required header would work.

Thus, I executed the following commands:

$customModulePaths = @('\\myServer\myShare\subfolder\module1\',
                       '\\myServer\myShare\subfolder\module2\')

$env:PSModulePath = $env:PSModulePath + [System.IO.Path]::PathSeparator + $($customModulePaths -join [System.IO.Path]::PathSeparator)

The scripts themselves are:

  • \\myServer\myShare\subfolder\module1\module1.psm1
  • \\myServer\myShare\subfolder\module2\module2.psm1

The module name is the same as the parent folder, and they have a manifest file (.psd1). I verified that the environment variable was indeed modified by checking the first command again.

Next, I executed Get-Module -ListAvailable -Refresh, expecting to see the 2 modules appear. However, the command's output only references items located in the original $env:PSModulePath's folders. Executing Import-Module module1 also results in an error, stating that the module can't be found.

Am I missing something or is what I'm trying to do not possible? Or is it due to the fact that I'm using UNC instead of local paths?

6
  • is there a corresponding module manifest for each module as well (ie. \\myServer\myShare\subfolder\module1\module1.psd1)? Commented May 12, 2020 at 12:36
  • @MathiasR.Jessen Yes (noted halfway), they each have a manifest generated using New-ModuleManifest. Commented May 12, 2020 at 12:37
  • Can the modules be imported successfully with Import-Module \\myServer\myShare\subfolder\module1 and Import-Module \\myServer\myShare\subfolder\module2? Commented May 12, 2020 at 12:39
  • Yes, both can be imported when referencing the module folder (both the statements are successful) Commented May 12, 2020 at 12:45
  • 1
    @ƘɌỈSƬƠƑ I see the problem now. You need to include \\myServer\myShare\subfolder, not the individual module folders Commented May 12, 2020 at 12:49

1 Answer 1

2

Look at the module paths already in $env:PSModulePath - notice that there are no actual module folders - only root folders that contain module folders.

Add the parent of the module directories instead and it should work:

$env:PSModulePath = $env:PSModulePath,'\\myServer\myShare\subfolder' -join [System.IO.Path]::PathSeparator
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.