powershell

Powershell: Installing Custom-Module with Install-Module


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?


Solution

  • 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
    

    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.


    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.