Reputation: 679
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
?
Upvotes: 3
Views: 6287
Reputation: 10019
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 etcInstall-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.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.
Upvotes: 4
Reputation: 1999
you can store the module manually in one of the modules paths.
the variable $Env:PSModulePath
shows the paths powershell looks for modules.
you can store the moduel for your account only at %UserProfile%\Documents\WindowsPowerShell\Modules
for reference the documentaion: https://learn.microsoft.com/en-us/powershell/scripting/developer/module/installing-a-powershell-module?view=powershell-7.1
Upvotes: 1