How can I make my powershell module import permanent?

7,649

Solution 1

You could also use the user profile script (that runs every time powershell starts) to run any commands you want including importing modules.

Located at C:\Users\USER_NAME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 or through via the powershell $Profile variable.

Changes only apply to that user account on that computer, so don't forget that if you are developing scripts for others to use.

Solution 2

Import-Module

The Import-Module cmdlet adds one or more modules to the current session. The modules that you import must be installed on the local computer or a remote computer.

So it's not supposed to be a permanent change and the command also doesn't imply it. You could add the load command to your profile. If you didn't need to use SkipEditionCheck it also would probably work by just using tab completion and auto import/load.

See about_Profiles for information about profiles. Change the version to match the one you use.

Share:
7,649

Related videos on Youtube

not2qubit
Author by

not2qubit

Updated on September 18, 2022

Comments

  • not2qubit
    not2qubit almost 2 years

    On an older Windows 8.1 machine, I have to use Import-Module -SkipEditionCheck Storage every time I want to use VHD related commands from Powershell. But unlike what the command seem to imply, it's not actually importing the module, but merely loading it. Because I need to run it every time I start pwsh.

    How can I make that module import stay permanent?


    UPDATE: 2020-01-26

    From the About Modules page:

    Also, commands that use PowerShell providers do not automatically import a module. For example, if you use a command that requires the WSMan: drive, such as the Get-PSSessionConfiguration cmdlet, you might need to run the Import-Module cmdlet to import the Microsoft.WSMan.Management module that includes the WSMan: drive.

    You can still run the Import-Module command to import a module and use the $PSModuleAutoloadingPreference variable to enable, disable and configure automatic importing of modules. For more information, see ...

    And from the About Preferences page for $PSModuleAutoloadingPreference:

    Enables and disables automatic importing of modules in the session. All is the default. Regardless of the variable's value, you can use Import-Module to import a module.

    Valid values are:
    All : Modules are imported automatically on first-use. To import a module, get or use any command in the module. For example, use Get-Command.

    ModuleQualified : Modules are imported automatically only when a user uses the module-qualified name of a command in the module. For example, if the user types MyModule\MyCommand, PowerShell imports the MyModule module.

    None: Automatic importing of modules is disabled in the session. To import a module, use the Import-Module cmdlet.

    However, my $PSModuleAutoloadingPreference is empty...

  • not2qubit
    not2qubit over 4 years
    That's interesting because when using on other shell versions on the same system the command are available, but there are no such imports in their startup profiles, AFAICT! So it must be done somewhere else...
  • Seth
    Seth over 4 years
    Yes, because there is auto loading (starting with v3). But if you do have other shell versions available why would you pick one that is incompatible with the commands you would like to run? See about_Modules.
  • not2qubit
    not2qubit over 4 years
    I'm running different shells for different purposes on production machines. Notably PS 7.0+ for development & testing.
  • not2qubit
    not2qubit over 4 years
    Seem that my $PSModuleAutoloadingPreference is empty... I wonder why?
  • Seth
    Seth over 4 years
    I don*t know. But my assumption would still be that the issue stems from having to ignore compatibility to load the module. PowerShell probably doesn't look for commands in modules that are marked incompatible. The configuration variable seems to be empty for me as well.