3

I have written a Powershell module. I would like to use this permanently. I understand that an Import-Module keeps the module in memory only during the session and is volatile.

It becomes persistent only by Install-Module. Unfortunately, Powershell expects a repository. I don't have that. Although I could manually copy the module to the module directory by determinating the path with $env:Path. But this is not nice.

Is there a "clean" way to install a custom module using Install-Module?

2 Answers 2

4

It becomes persistent only by Install-Module

This is only because Install-Module copies the module to one of the locations in $env:PSModulePath. It doesn't do anything clever to made the module persistent.

PowerShell reads the module manifests (.psd1 files) in those locations and any functions in the FunctionsToExport will be available.

When you try to use one of those functions, PowerShell will import that module.
You can test this out by running Get-Module before and after using a function from an unloaded module.
You can also test the fact that it has to import the module by adding a throw to the top of your .psm1 file - trying to use a function from that module will give you:

Custom-Command : The 'Custom-Command' command was found in the module 'Custom-Module', but the module could not be loaded. For more information, run 'Import-Module Custom-Module'.


Unfortunately, Powershell expects a repository. I don't have that

You can create one.

$customModulePath = 'C:\Temp\Modules\Custom-Module'
$repoPath = 'C:\Temp\LocalRepo'
New-Item -Path $repoPath -ItemType Directory

$repoParameters = @{
    Name               = 'LocalRepo'
    SourceLocation     = $repoPath
    PublishLocation    = $repoPath
    InstallationPolicy = 'Trusted'
}
Register-PSRepository @repoParameters
Publish-Module -Path $customModulePath -Repository LocalRepo
Install-Module Custom-Module -Repository LocalRepo
  • Publish-Module will do some validation on your module and fail if it sees anything it doesn't like.
  • Publish-Module will create a zip of you module in $repoPath - you can open it using 7zip etc
  • Install-Module will install in the AllUsers scope by default if it can (running as admin) and CurrentUser if it can't. You can use the -Scope parameter to override this.
  • As detailed in Install-module documentation:
    AllUsers path: $env:ProgramFiles\PowerShell\Modules
    CurrentUser path: $home\Documents\PowerShell\Modules

Your module will be in one of the paths above, exactly as if you had copied it. The only difference will be an additional (hidden) PSGetModuleInfo.xml file. This has information related to the module package and repository.


Although I could manually copy the module to the module directory by determinating the path with $env:Path. But this is not nice.

I think this is a viable option, using one of the paths above, as that's the part of Install-Module that gives you persistence.

Another way is to modify your $profile to import the module at start or add your module directory to $env:PSModulePath. I use the second one so I can keep my modules in my repo with the rest of my code.

  • Import-Module 'C:\Temp\Modules\Custom-Module'
  • $env:PSModulePath += ';C:\Temp\Modules\'

Is there a "clean" way to install a custom module using Install-Module?

If by "clean" you mean Install-Module 'C:\Temp\Modules\Custom-Module', then no - that's not what Install-Module is designed for.
If you're after module persistence, that's a yes.

Sign up to request clarification or add additional context in comments.

1 Comment

wow this seems like a lot more work than npm install -g for my custom node script.
1

you can store the module manually in one of the modules paths.

the variable $Env:PSModulePath shows the paths PowerShell looks in for modules.

you can store the module for your account only at %UserProfile%\Documents\WindowsPowerShell\Modules

for reference the documentation: https://learn.microsoft.com/en-us/powershell/scripting/developer/module/installing-a-powershell-module?view=powershell-7.1

1 Comment

That's exactly what I don't want. But I understand there seems only two options: 1. Installing manually 2. Using a Repository

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.