7

I have a script I wish to use interactively from the PowerShell prompt. The script needs to use a local script module.

I cannot see how to import/use the module such that it's not left loaded in the current session.

Example

A module (MyModule.psm1)...

function Test-Method
{
    write-host "Test-Method invoked"
}

... and a script (script.ps1)

Import-Module .\MyModule
Test-Method

Now running the script at the PowerShell prompt ...

PS C:\temp> Get-Module | % {$_.Name}
Microsoft.PowerShell.Management
Microsoft.PowerShell.Utility

PS C:\temp> .\script.ps1
Test-Method invoked

PS C:\temp> Get-Module | % {$_.Name}
Microsoft.PowerShell.Management
Microsoft.PowerShell.Utility
MyModule

How can my script import and use MyModule.psm1 without it being left loaded in the caller's current session? Bearing in mind that the call may have already imported the module and would not want it unloaded by the script (so simply removing the module at the completion of the script is not really good enough).

I've considered dot-sourcing the module rather than importing it, but I want the module for the reasons covered in PowerShell Import-Module vs Dot Sourcing

4 Answers 4

8

It sounds like you already described in pseudo-code what you wanted. Here it is in actual code:

$checkCmds = Get-Commands -Module MyModule

Import-Module MyModule

# Do stuff here . . .

# unload only if we loaded it
if ($checkCmds -eq $null) { Remove-Module MyModule }
3
  • 1
    Yeah, good point. For some reason, this didn't feel quite right, although I can't see any obvious reason why not. I guess I was hoping for some straightforward way for the import to be done in the script's own scope. Oct 2, 2013 at 23:37
  • I think this meets both of those criteria: it seems quite straightforward to me and can certainly be done within a script... so what else would your conceptually ideal solution offer? Oct 3, 2013 at 13:11
  • 1
    Get-Commands should be Get-Command. (I'd prefer to just edit the post to fix it, but turns out there's a 6 character change minimum for edits)
    – tdashroy
    Dec 2, 2019 at 23:22
1

As far as I can tell, you don't get that automatic cleanup behavior from a "script" importing a module. OTOH if you import a module from within another module, when the parent module is removed then any modules it imported will be removed if there are no other modules using them (or unless ipmo -global was specified).

3
  • That's more or less where I'd got to. To me it doesn't sit at all well with using PowerShell as a (traditional) shell - to the extent I was sure I was missing something. Oct 1, 2013 at 2:53
  • What does "ipmo" stand for? I've never seen that and Google isn't helping too much.
    – jpmc26
    Mar 2, 2017 at 1:49
  • 1
    @jpmc26 ipmo is an alias for Import-Module.
    – Keith Hill
    Mar 2, 2017 at 21:43
1

This builds on the previous answer and uses the following property.

If you import a module from within another module, when the parent module is removed then any modules it imported will be removed

You can exploit several techniques to create a wrapper:

  • Importing a module from a module
  • Anonymous modules
  • Call operator with the context of a module

Set script.ps1 to

& (New-Module {
    function Invoke-Function {
        Import-Module .\MyModule
        Test-Method
    }
}) { Invoke-Function }

If you run script.ps1 and then (Get-Module).Name then MyModule will not be listed in the output.

Note: In this example Invoke-Function is just another scope, and can be omitted, letting the New-Module just run when defined. In one line:

& (New-Module { Import-Module .\MyModule; Test-Method }) {}
-1

You can import the module with -Scope local to restrict a module to your script's scope. If the module happens to also be loaded in the global scope, then it will still be available after your script exits.

1
  • 1
    This worked for me with one caveat: After the script exited the functions declared in the module were not accessible (as desired) however the module still appeared when running Get-Module.
    – John Rees
    May 16, 2017 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.